Merge pull request #333 from warpc/195-new_design_for_projects

[issue #195] Fixed <Tab> in edit blobs.
This commit is contained in:
Vladimir Sharshov 2012-03-22 09:38:27 -07:00
commit f54ec81734
2 changed files with 154 additions and 83 deletions

View File

@ -19,5 +19,8 @@
:javascript :javascript
$(function() { $(function() {
var editor = CodeMirror.fromTextArea(document.getElementById('code'), {lineNumbers: true, mode: '#{@blob.raw_mime_type.content_type}'}); var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
lineNumbers: true,
mode: '#{@blob.raw_mime_type.content_type}'
});
}); });

View File

@ -1,5 +1,3 @@
// CodeMirror version 2.22
//
// All functions that need access to the editor's state live inside // All functions that need access to the editor's state live inside
// the CodeMirror function. Below that, at the bottom of the file, // the CodeMirror function. Below that, at the bottom of the file,
// some utilities are defined. // some utilities are defined.
@ -15,9 +13,8 @@ var CodeMirror = (function() {
if (defaults.hasOwnProperty(opt)) if (defaults.hasOwnProperty(opt))
options[opt] = (givenOptions && givenOptions.hasOwnProperty(opt) ? givenOptions : defaults)[opt]; options[opt] = (givenOptions && givenOptions.hasOwnProperty(opt) ? givenOptions : defaults)[opt];
var targetDocument = options["document"];
// The element in which the editor lives. // The element in which the editor lives.
var wrapper = targetDocument.createElement("div"); var wrapper = document.createElement("div");
wrapper.className = "CodeMirror" + (options.lineWrapping ? " CodeMirror-wrap" : ""); wrapper.className = "CodeMirror" + (options.lineWrapping ? " CodeMirror-wrap" : "");
// This mess creates the base DOM structure for the editor. // This mess creates the base DOM structure for the editor.
wrapper.innerHTML = wrapper.innerHTML =
@ -48,7 +45,10 @@ var CodeMirror = (function() {
if (!webkit) lineSpace.draggable = true; if (!webkit) lineSpace.draggable = true;
lineSpace.style.outline = "none"; lineSpace.style.outline = "none";
if (options.tabindex != null) input.tabIndex = options.tabindex; if (options.tabindex != null) input.tabIndex = options.tabindex;
if (options.autofocus) focusInput();
if (!options.gutter && !options.lineNumbers) gutter.style.display = "none"; if (!options.gutter && !options.lineNumbers) gutter.style.display = "none";
// Needed to handle Tab key in KHTML
if (khtml) inputDiv.style.height = "1px", inputDiv.style.position = "absolute";
// Check for problem with IE innerHTML not working when we have a // Check for problem with IE innerHTML not working when we have a
// P (or similar) parent node. // P (or similar) parent node.
@ -81,12 +81,13 @@ var CodeMirror = (function() {
gutterDirty, callbacks; gutterDirty, callbacks;
// Current visible range (may be bigger than the view window). // Current visible range (may be bigger than the view window).
var displayOffset = 0, showingFrom = 0, showingTo = 0, lastSizeC = 0; var displayOffset = 0, showingFrom = 0, showingTo = 0, lastSizeC = 0;
// bracketHighlighted is used to remember that a backet has been // bracketHighlighted is used to remember that a bracket has been
// marked. // marked.
var bracketHighlighted; var bracketHighlighted;
// Tracks the maximum line length so that the horizontal scrollbar // Tracks the maximum line length so that the horizontal scrollbar
// can be kept static when scrolling. // can be kept static when scrolling.
var maxLine = "", maxWidth; var maxLine = "", maxWidth;
var tabCache = {};
// Initialize the content. // Initialize the content.
operation(function(){setValue(options.value || ""); updateInput = false;})(); operation(function(){setValue(options.value || ""); updateInput = false;})();
@ -124,10 +125,16 @@ var CodeMirror = (function() {
if (!options.readOnly) replaceSelection(""); if (!options.readOnly) replaceSelection("");
})); }));
// Needed to handle Tab key in KHTML
if (khtml) connect(code, "mouseup", function() {
if (document.activeElement == input) input.blur();
focusInput();
});
// IE throws unspecified error in certain cases, when // IE throws unspecified error in certain cases, when
// trying to access activeElement before onload // trying to access activeElement before onload
var hasFocus; try { hasFocus = (targetDocument.activeElement == input); } catch(e) { } var hasFocus; try { hasFocus = (document.activeElement == input); } catch(e) { }
if (hasFocus) setTimeout(onFocus, 20); if (hasFocus || options.autofocus) setTimeout(onFocus, 20);
else onBlur(); else onBlur();
function isLine(l) {return l >= 0 && l < doc.size;} function isLine(l) {return l >= 0 && l < doc.size;}
@ -178,17 +185,23 @@ var CodeMirror = (function() {
line = clipLine(line == null ? doc.size - 1: line); line = clipLine(line == null ? doc.size - 1: line);
return getStateBefore(line + 1); return getStateBefore(line + 1);
}, },
cursorCoords: function(start){ cursorCoords: function(start, mode) {
if (start == null) start = sel.inverted; if (start == null) start = sel.inverted;
return pageCoords(start ? sel.from : sel.to); return this.charCoords(start ? sel.from : sel.to, mode);
},
charCoords: function(pos, mode) {
pos = clipPos(pos);
if (mode == "local") return localCoords(pos, false);
if (mode == "div") return localCoords(pos, true);
return pageCoords(pos);
}, },
charCoords: function(pos){return pageCoords(clipPos(pos));},
coordsChar: function(coords) { coordsChar: function(coords) {
var off = eltOffset(lineSpace); var off = eltOffset(lineSpace);
return coordsChar(coords.x - off.left, coords.y - off.top); return coordsChar(coords.x - off.left, coords.y - off.top);
}, },
markText: operation(markText), markText: operation(markText),
setBookmark: setBookmark, setBookmark: setBookmark,
findMarksAt: findMarksAt,
setMarker: operation(addGutterMarker), setMarker: operation(addGutterMarker),
clearMarker: operation(removeGutterMarker), clearMarker: operation(removeGutterMarker),
setLineClass: operation(setLineClass), setLineClass: operation(setLineClass),
@ -256,6 +269,7 @@ var CodeMirror = (function() {
replaceRange: operation(replaceRange), replaceRange: operation(replaceRange),
getRange: function(from, to) {return getRange(clipPos(from), clipPos(to));}, getRange: function(from, to) {return getRange(clipPos(from), clipPos(to));},
triggerOnKeyDown: operation(onKeyDown),
execCommand: function(cmd) {return commands[cmd](instance);}, execCommand: function(cmd) {return commands[cmd](instance);},
// Stuff used by commands, probably not much use to outside code. // Stuff used by commands, probably not much use to outside code.
moveH: operation(moveH), moveH: operation(moveH),
@ -373,7 +387,7 @@ var CodeMirror = (function() {
!posLess(start, sel.from) && !posLess(sel.to, start)) { !posLess(start, sel.from) && !posLess(sel.to, start)) {
// Let the drag handler handle this. // Let the drag handler handle this.
if (webkit) lineSpace.draggable = true; if (webkit) lineSpace.draggable = true;
var up = connect(targetDocument, "mouseup", operation(function(e2) { var up = connect(document, "mouseup", operation(function(e2) {
if (webkit) lineSpace.draggable = false; if (webkit) lineSpace.draggable = false;
draggingText = false; draggingText = false;
up(); up();
@ -384,6 +398,8 @@ var CodeMirror = (function() {
} }
}), true); }), true);
draggingText = true; draggingText = true;
// IE's approach to draggable
if (lineSpace.dragDrop) lineSpace.dragDrop();
return; return;
} }
e_preventDefault(e); e_preventDefault(e);
@ -402,12 +418,7 @@ var CodeMirror = (function() {
} }
} }
var move = connect(targetDocument, "mousemove", operation(function(e) { function done(e) {
clearTimeout(going);
e_preventDefault(e);
extend(e);
}), true);
var up = connect(targetDocument, "mouseup", operation(function(e) {
clearTimeout(going); clearTimeout(going);
var cur = posFromMouse(e); var cur = posFromMouse(e);
if (cur) setSelectionUser(start, cur); if (cur) setSelectionUser(start, cur);
@ -415,7 +426,14 @@ var CodeMirror = (function() {
focusInput(); focusInput();
updateInput = true; updateInput = true;
move(); up(); move(); up();
}
var move = connect(document, "mousemove", operation(function(e) {
clearTimeout(going);
e_preventDefault(e);
if (!ie && !e_button(e)) done(e);
else extend(e);
}), true); }), true);
var up = connect(document, "mouseup", operation(done), true);
} }
function onDoubleClick(e) { function onDoubleClick(e) {
for (var n = e_target(e); n != wrapper; n = n.parentNode) for (var n = e_target(e); n != wrapper; n = n.parentNode)
@ -464,11 +482,14 @@ var CodeMirror = (function() {
} }
function onDragStart(e) { function onDragStart(e) {
var txt = getSelection(); var txt = getSelection();
// Disabled until further notice. Doesn't work on most browsers,
// and crashes Safari (issue #332).
//htmlEscape(txt);
//e.dataTransfer.setDragImage(escapeElement, 0, 0);
e.dataTransfer.setData("Text", txt); e.dataTransfer.setData("Text", txt);
// Use dummy image instead of default browsers image.
if (gecko || chrome) {
var img = document.createElement('img');
img.scr = 'data:image/gif;base64,R0lGODdhAgACAIAAAAAAAP///ywAAAAAAgACAAACAoRRADs='; //1x1 image
e.dataTransfer.setDragImage(img, 0, 0);
}
} }
function doHandleBinding(bound, dropShift) { function doHandleBinding(bound, dropShift) {
@ -506,13 +527,19 @@ var CodeMirror = (function() {
if (e_prop(e, "ctrlKey")) name = "Ctrl-" + name; if (e_prop(e, "ctrlKey")) name = "Ctrl-" + name;
if (e_prop(e, "metaKey")) name = "Cmd-" + name; if (e_prop(e, "metaKey")) name = "Cmd-" + name;
if (e_prop(e, "shiftKey")) if (e_prop(e, "shiftKey")) {
handled = lookupKey("Shift-" + name, options.extraKeys, options.keyMap, handled = lookupKey("Shift-" + name, options.extraKeys, options.keyMap,
function(b) {return doHandleBinding(b, true);}); function(b) {return doHandleBinding(b, true);})
if (!handled) || lookupKey(name, options.extraKeys, options.keyMap, function(b) {
if (typeof b == "string" && /^go[A-Z]/.test(b)) return doHandleBinding(b);
});
} else {
handled = lookupKey(name, options.extraKeys, options.keyMap, doHandleBinding); handled = lookupKey(name, options.extraKeys, options.keyMap, doHandleBinding);
}
if (handled) e_preventDefault(e); if (handled) {
e_preventDefault(e);
if (ie) { e.oldKeyCode = e.keyCode; e.keyCode = 0; }
}
return handled; return handled;
} }
function handleCharBinding(e, ch) { function handleCharBinding(e, ch) {
@ -545,7 +572,7 @@ var CodeMirror = (function() {
if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e))) return; if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e))) return;
var keyCode = e_prop(e, "keyCode"), charCode = e_prop(e, "charCode"); var keyCode = e_prop(e, "keyCode"), charCode = e_prop(e, "charCode");
if (window.opera && keyCode == lastStoppedKey) {lastStoppedKey = null; e_preventDefault(e); return;} if (window.opera && keyCode == lastStoppedKey) {lastStoppedKey = null; e_preventDefault(e); return;}
if (window.opera && !e.which && handleKeyBinding(e)) return; if (((window.opera && !e.which) || khtml) && handleKeyBinding(e)) return;
var ch = String.fromCharCode(charCode == null ? keyCode : charCode); var ch = String.fromCharCode(charCode == null ? keyCode : charCode);
if (options.electricChars && mode.electricChars && options.smartIndent && !options.readOnly) { if (options.electricChars && mode.electricChars && options.smartIndent && !options.readOnly) {
if (mode.electricChars.indexOf(ch) > -1) if (mode.electricChars.indexOf(ch) > -1)
@ -837,11 +864,11 @@ var CodeMirror = (function() {
return scrollIntoView(x, cursor.y, x, cursor.yBot); return scrollIntoView(x, cursor.y, x, cursor.yBot);
} }
function scrollIntoView(x1, y1, x2, y2) { function scrollIntoView(x1, y1, x2, y2) {
var pl = paddingLeft(), pt = paddingTop(), lh = textHeight(); var pl = paddingLeft(), pt = paddingTop();
y1 += pt; y2 += pt; x1 += pl; x2 += pl; y1 += pt; y2 += pt; x1 += pl; x2 += pl;
var screen = scroller.clientHeight, screentop = scroller.scrollTop, scrolled = false, result = true; var screen = scroller.clientHeight, screentop = scroller.scrollTop, scrolled = false, result = true;
if (y1 < screentop) {scroller.scrollTop = Math.max(0, y1 - 2*lh); scrolled = true;} if (y1 < screentop) {scroller.scrollTop = Math.max(0, y1); scrolled = true;}
else if (y2 > screentop + screen) {scroller.scrollTop = y2 + lh - screen; scrolled = true;} else if (y2 > screentop + screen) {scroller.scrollTop = y2 - screen; scrolled = true;}
var screenw = scroller.clientWidth, screenleft = scroller.scrollLeft; var screenw = scroller.clientWidth, screenleft = scroller.scrollLeft;
var gutterw = options.fixedGutter ? gutter.clientWidth : 0; var gutterw = options.fixedGutter ? gutter.clientWidth : 0;
@ -996,16 +1023,17 @@ var CodeMirror = (function() {
} }
// This pass fills in the lines that actually changed. // This pass fills in the lines that actually changed.
var nextIntact = intact.shift(), curNode = lineDiv.firstChild, j = from; var nextIntact = intact.shift(), curNode = lineDiv.firstChild, j = from;
var scratch = targetDocument.createElement("div"), newElt; var scratch = document.createElement("div");
doc.iter(from, to, function(line) { doc.iter(from, to, function(line) {
if (nextIntact && nextIntact.to == j) nextIntact = intact.shift(); if (nextIntact && nextIntact.to == j) nextIntact = intact.shift();
if (!nextIntact || nextIntact.from > j) { if (!nextIntact || nextIntact.from > j) {
if (line.hidden) var html = scratch.innerHTML = "<pre></pre>"; if (line.hidden) var html = scratch.innerHTML = "<pre></pre>";
else { else {
var html = '<pre>' + line.getHTML(makeTab) + '</pre>'; var html = '<pre' + (line.className ? ' class="' + line.className + '"' : '') + '>'
+ line.getHTML(makeTab) + '</pre>';
// Kludge to make sure the styled element lies behind the selection (by z-index) // Kludge to make sure the styled element lies behind the selection (by z-index)
if (line.className) if (line.bgClassName)
html = '<div style="position: relative"><pre class="' + line.className + html = '<div style="position: relative"><pre class="' + line.bgClassName +
'" style="position: absolute; left: 0; right: 0; top: 0; bottom: 0; z-index: -2">&#160;</pre>' + html + "</div>"; '" style="position: absolute; left: 0; right: 0; top: 0; bottom: 0; z-index: -2">&#160;</pre>' + html + "</div>";
} }
scratch.innerHTML = html; scratch.innerHTML = html;
@ -1021,7 +1049,7 @@ var CodeMirror = (function() {
if (!options.gutter && !options.lineNumbers) return; if (!options.gutter && !options.lineNumbers) return;
var hText = mover.offsetHeight, hEditor = scroller.clientHeight; var hText = mover.offsetHeight, hEditor = scroller.clientHeight;
gutter.style.height = (hText - hEditor < 2 ? hEditor : hText) + "px"; gutter.style.height = (hText - hEditor < 2 ? hEditor : hText) + "px";
var html = [], i = showingFrom; var html = [], i = showingFrom, normalNode;
doc.iter(showingFrom, Math.max(showingTo, showingFrom + 1), function(line) { doc.iter(showingFrom, Math.max(showingTo, showingFrom + 1), function(line) {
if (line.hidden) { if (line.hidden) {
html.push("<pre></pre>"); html.push("<pre></pre>");
@ -1035,14 +1063,19 @@ var CodeMirror = (function() {
html.push((marker && marker.style ? '<pre class="' + marker.style + '">' : "<pre>"), text); html.push((marker && marker.style ? '<pre class="' + marker.style + '">' : "<pre>"), text);
for (var j = 1; j < line.height; ++j) html.push("<br/>&#160;"); for (var j = 1; j < line.height; ++j) html.push("<br/>&#160;");
html.push("</pre>"); html.push("</pre>");
if (!marker) normalNode = i;
} }
++i; ++i;
}); });
gutter.style.display = "none"; gutter.style.display = "none";
gutterText.innerHTML = html.join(""); gutterText.innerHTML = html.join("");
var minwidth = String(doc.size).length, firstNode = gutterText.firstChild, val = eltText(firstNode), pad = ""; // Make sure scrolling doesn't cause number gutter size to pop
while (val.length + pad.length < minwidth) pad += "\u00a0"; if (normalNode != null) {
if (pad) firstNode.insertBefore(targetDocument.createTextNode(pad), firstNode.firstChild); var node = gutterText.childNodes[normalNode - showingFrom];
var minwidth = String(doc.size).length, val = eltText(node), pad = "";
while (val.length + pad.length < minwidth) pad += "\u00a0";
if (pad) node.insertBefore(document.createTextNode(pad), node.firstChild);
}
gutter.style.display = ""; gutter.style.display = "";
lineSpace.style.marginLeft = gutter.offsetWidth + "px"; lineSpace.style.marginLeft = gutter.offsetWidth + "px";
gutterDirty = false; gutterDirty = false;
@ -1066,16 +1099,18 @@ var CodeMirror = (function() {
html += '<div class="CodeMirror-selected" style="position: absolute; left: ' + left + html += '<div class="CodeMirror-selected" style="position: absolute; left: ' + left +
'px; top: ' + top + 'px; right: ' + right + 'px; height: ' + height + 'px"></div>'; 'px; top: ' + top + 'px; right: ' + right + 'px; height: ' + height + 'px"></div>';
} }
var clientWidth = lineSpace.clientWidth || lineSpace.offsetWidth;
var clientHeight = lineSpace.clientHeight || lineSpace.offsetHeight;
if (sel.from.ch && fromPos.y >= 0) { if (sel.from.ch && fromPos.y >= 0) {
var right = sameLine ? lineSpace.clientWidth - toPos.x : 0; var right = sameLine ? clientWidth - toPos.x : 0;
add(fromPos.x, fromPos.y, right, th); add(fromPos.x, fromPos.y, right, th);
} }
var middleStart = Math.max(0, fromPos.y + (sel.from.ch ? th : 0)); var middleStart = Math.max(0, fromPos.y + (sel.from.ch ? th : 0));
var middleHeight = Math.min(toPos.y, lineSpace.clientHeight) - middleStart; var middleHeight = Math.min(toPos.y, clientHeight) - middleStart;
if (middleHeight > 0.2 * th) if (middleHeight > 0.2 * th)
add(0, middleStart, 0, middleHeight); add(0, middleStart, 0, middleHeight);
if ((!sameLine || !sel.from.ch) && toPos.y < lineSpace.clientHeight - .5 * th) if ((!sameLine || !sel.from.ch) && toPos.y < clientHeight - .5 * th)
add(0, toPos.y, lineSpace.clientWidth - toPos.x, th); add(0, toPos.y, clientWidth - toPos.x, th);
selectionDiv.innerHTML = html; selectionDiv.innerHTML = html;
cursor.style.display = "none"; cursor.style.display = "none";
selectionDiv.style.display = ""; selectionDiv.style.display = "";
@ -1105,7 +1140,12 @@ var CodeMirror = (function() {
if (posLess(to, from)) {var tmp = to; to = from; from = tmp;} if (posLess(to, from)) {var tmp = to; to = from; from = tmp;}
// Skip over hidden lines. // Skip over hidden lines.
if (from.line != oldFrom) from = skipHidden(from, oldFrom, sel.from.ch); if (from.line != oldFrom) {
var from1 = skipHidden(from, oldFrom, sel.from.ch);
// If there is no non-hidden line left, force visibility on current line
if (!from1) setLineHidden(from.line, false);
else from = from1;
}
if (to.line != oldTo) to = skipHidden(to, oldTo, sel.to.ch); if (to.line != oldTo) to = skipHidden(to, oldTo, sel.to.ch);
if (posEq(from, to)) sel.inverted = false; if (posEq(from, to)) sel.inverted = false;
@ -1114,7 +1154,7 @@ var CodeMirror = (function() {
if (options.autoClearEmptyLines && posEq(sel.from, sel.to)) { if (options.autoClearEmptyLines && posEq(sel.from, sel.to)) {
var head = sel.inverted ? from : to; var head = sel.inverted ? from : to;
if (head.line != sel.from.line) { if (head.line != sel.from.line && sel.from.line < doc.size) {
var oldLine = getLine(sel.from.line); var oldLine = getLine(sel.from.line);
if (/^\s+$/.test(oldLine.text)) if (/^\s+$/.test(oldLine.text))
setTimeout(operation(function() { setTimeout(operation(function() {
@ -1209,6 +1249,7 @@ var CodeMirror = (function() {
if (unit == "page") dist = Math.min(scroller.clientHeight, window.innerHeight || document.documentElement.clientHeight); if (unit == "page") dist = Math.min(scroller.clientHeight, window.innerHeight || document.documentElement.clientHeight);
else if (unit == "line") dist = textHeight(); else if (unit == "line") dist = textHeight();
var target = coordsChar(pos.x, pos.y + dist * dir + 2); var target = coordsChar(pos.x, pos.y + dist * dir + 2);
if (unit == "page") scroller.scrollTop += localCoords(target, true).y - pos.y;
setCursor(target.line, target.ch, true); setCursor(target.line, target.ch, true);
goalColumn = pos.x; goalColumn = pos.x;
} }
@ -1295,9 +1336,10 @@ var CodeMirror = (function() {
changes.push({from: 0, to: doc.size}); changes.push({from: 0, to: doc.size});
} }
function makeTab(col) { function makeTab(col) {
var w = options.tabSize - col % options.tabSize; var w = options.tabSize - col % options.tabSize, cached = tabCache[w];
if (cached) return cached;
for (var str = '<span class="cm-tab">', i = 0; i < w; ++i) str += " "; for (var str = '<span class="cm-tab">', i = 0; i < w; ++i) str += " ";
return {html: str + "</span>", width: w}; return (tabCache[w] = {html: str + "</span>", width: w});
} }
function themeChanged() { function themeChanged() {
scroller.className = scroller.className.replace(/\s*cm-s-\w+/g, "") + scroller.className = scroller.className.replace(/\s*cm-s-\w+/g, "") +
@ -1313,7 +1355,7 @@ var CodeMirror = (function() {
var lineN = lineNo(line); var lineN = lineNo(line);
min = Math.min(min, lineN); max = Math.max(max, lineN); min = Math.min(min, lineN); max = Math.max(max, lineN);
for (var j = 0; j < mk.length; ++j) for (var j = 0; j < mk.length; ++j)
if (mk[j].set == this.set) mk.splice(j--, 1); if (mk[j].marker == this) mk.splice(j--, 1);
} }
if (min != Infinity) if (min != Infinity)
changes.push({from: min, to: max + 1}); changes.push({from: min, to: max + 1});
@ -1324,7 +1366,7 @@ var CodeMirror = (function() {
var line = this.set[i], mk = line.marked; var line = this.set[i], mk = line.marked;
for (var j = 0; j < mk.length; ++j) { for (var j = 0; j < mk.length; ++j) {
var mark = mk[j]; var mark = mk[j];
if (mark.set == this.set) { if (mark.marker == this) {
if (mark.from != null || mark.to != null) { if (mark.from != null || mark.to != null) {
var found = lineNo(line); var found = lineNo(line);
if (found != null) { if (found != null) {
@ -1341,8 +1383,9 @@ var CodeMirror = (function() {
function markText(from, to, className) { function markText(from, to, className) {
from = clipPos(from); to = clipPos(to); from = clipPos(from); to = clipPos(to);
var tm = new TextMarker(); var tm = new TextMarker();
if (!posLess(from, to)) return tm;
function add(line, from, to, className) { function add(line, from, to, className) {
getLine(line).addMark(new MarkedText(from, to, className, tm.set)); getLine(line).addMark(new MarkedText(from, to, className, tm));
} }
if (from.line == to.line) add(from.line, from.ch, to.ch, className); if (from.line == to.line) add(from.line, from.ch, to.ch, className);
else { else {
@ -1362,6 +1405,19 @@ var CodeMirror = (function() {
return bm; return bm;
} }
function findMarksAt(pos) {
pos = clipPos(pos);
var markers = [], marked = getLine(pos.line).marked;
if (!marked) return markers;
for (var i = 0, e = marked.length; i < e; ++i) {
var m = marked[i];
if ((m.from == null || m.from <= pos.ch) &&
(m.to == null || m.to >= pos.ch))
markers.push(m.marker || m);
}
return markers;
}
function addGutterMarker(line, text, className) { function addGutterMarker(line, text, className) {
if (typeof line == "number") line = getLine(clipLine(line)); if (typeof line == "number") line = getLine(clipLine(line));
line.gutterMarker = {text: text, style: className}; line.gutterMarker = {text: text, style: className};
@ -1383,10 +1439,11 @@ var CodeMirror = (function() {
else return null; else return null;
return line; return line;
} }
function setLineClass(handle, className) { function setLineClass(handle, className, bgClassName) {
return changeLine(handle, function(line) { return changeLine(handle, function(line) {
if (line.className != className) { if (line.className != className || line.bgClassName != bgClassName) {
line.className = className; line.className = className;
line.bgClassName = bgClassName;
return true; return true;
} }
}); });
@ -1400,6 +1457,8 @@ var CodeMirror = (function() {
if (hidden && (fline == no || tline == no)) { if (hidden && (fline == no || tline == no)) {
var from = fline == no ? skipHidden({line: fline, ch: 0}, fline, 0) : sel.from; var from = fline == no ? skipHidden({line: fline, ch: 0}, fline, 0) : sel.from;
var to = tline == no ? skipHidden({line: tline, ch: 0}, tline, 0) : sel.to; var to = tline == no ? skipHidden({line: tline, ch: 0}, tline, 0) : sel.to;
// Can't hide the last visible line, we'd have no place to put the cursor
if (!to) return;
setSelection(from, to); setSelection(from, to);
} }
return (gutterDirty = true); return (gutterDirty = true);
@ -1420,7 +1479,7 @@ var CodeMirror = (function() {
} }
var marker = line.gutterMarker; var marker = line.gutterMarker;
return {line: n, handle: line, text: line.text, markerText: marker && marker.text, return {line: n, handle: line, text: line.text, markerText: marker && marker.text,
markerClass: marker && marker.style, lineClass: line.className}; markerClass: marker && marker.style, lineClass: line.className, bgClass: line.bgClassName};
} }
function stringWidth(str) { function stringWidth(str) {
@ -1464,7 +1523,7 @@ var CodeMirror = (function() {
var extra = ""; var extra = "";
// Include extra text at the end to make sure the measured line is wrapped in the right way. // Include extra text at the end to make sure the measured line is wrapped in the right way.
if (options.lineWrapping) { if (options.lineWrapping) {
var end = line.text.indexOf(" ", ch + 2); var end = line.text.indexOf(" ", ch + 6);
extra = htmlEscape(line.text.slice(ch + 1, end < 0 ? line.text.length : end + (ie ? 5 : 0))); extra = htmlEscape(line.text.slice(ch + 1, end < 0 ? line.text.length : end + (ie ? 5 : 0)));
} }
measure.innerHTML = "<pre>" + line.getHTML(makeTab, ch) + measure.innerHTML = "<pre>" + line.getHTML(makeTab, ch) +
@ -1823,7 +1882,7 @@ var CodeMirror = (function() {
pollInterval: 100, pollInterval: 100,
undoDepth: 40, undoDepth: 40,
tabindex: null, tabindex: null,
document: window.document autofocus: null
}; };
var ios = /AppleWebKit/.test(navigator.userAgent) && /Mobile\/\w+/.test(navigator.userAgent); var ios = /AppleWebKit/.test(navigator.userAgent) && /Mobile\/\w+/.test(navigator.userAgent);
@ -1831,7 +1890,7 @@ var CodeMirror = (function() {
var win = /Win/.test(navigator.platform); var win = /Win/.test(navigator.platform);
// Known modes, by name and by MIME // Known modes, by name and by MIME
var modes = {}, mimeModes = {}; var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};
CodeMirror.defineMode = function(name, mode) { CodeMirror.defineMode = function(name, mode) {
if (!CodeMirror.defaults.mode && name != "null") CodeMirror.defaults.mode = name; if (!CodeMirror.defaults.mode && name != "null") CodeMirror.defaults.mode = name;
modes[name] = mode; modes[name] = mode;
@ -1842,6 +1901,8 @@ var CodeMirror = (function() {
CodeMirror.resolveMode = function(spec) { CodeMirror.resolveMode = function(spec) {
if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) if (typeof spec == "string" && mimeModes.hasOwnProperty(spec))
spec = mimeModes[spec]; spec = mimeModes[spec];
else if (typeof spec == "string" && /^[\w\-]+\/[\w\-]+\+xml$/.test(spec))
return CodeMirror.resolveMode("application/xml");
if (typeof spec == "string") return {name: spec}; if (typeof spec == "string") return {name: spec};
else return spec || {name: "null"}; else return spec || {name: "null"};
}; };
@ -1926,7 +1987,7 @@ var CodeMirror = (function() {
keyMap.basic = { keyMap.basic = {
"Left": "goCharLeft", "Right": "goCharRight", "Up": "goLineUp", "Down": "goLineDown", "Left": "goCharLeft", "Right": "goCharRight", "Up": "goLineUp", "Down": "goLineDown",
"End": "goLineEnd", "Home": "goLineStartSmart", "PageUp": "goPageUp", "PageDown": "goPageDown", "End": "goLineEnd", "Home": "goLineStartSmart", "PageUp": "goPageUp", "PageDown": "goPageDown",
"Delete": "delCharRight", "Backspace": "delCharLeft", "Tab": "indentMore", "Shift-Tab": "indentLess", "Delete": "delCharRight", "Backspace": "delCharLeft", "Tab": "insertTab", "Shift-Tab": "indentAuto",
"Enter": "newlineAndIndent", "Insert": "toggleOverwrite" "Enter": "newlineAndIndent", "Insert": "toggleOverwrite"
}; };
// Note that the save and find-related commands aren't defined by // Note that the save and find-related commands aren't defined by
@ -1937,6 +1998,7 @@ var CodeMirror = (function() {
"Ctrl-Left": "goWordLeft", "Ctrl-Right": "goWordRight", "Alt-Left": "goLineStart", "Alt-Right": "goLineEnd", "Ctrl-Left": "goWordLeft", "Ctrl-Right": "goWordRight", "Alt-Left": "goLineStart", "Alt-Right": "goLineEnd",
"Ctrl-Backspace": "delWordLeft", "Ctrl-Delete": "delWordRight", "Ctrl-S": "save", "Ctrl-F": "find", "Ctrl-Backspace": "delWordLeft", "Ctrl-Delete": "delWordRight", "Ctrl-S": "save", "Ctrl-F": "find",
"Ctrl-G": "findNext", "Shift-Ctrl-G": "findPrev", "Shift-Ctrl-F": "replace", "Shift-Ctrl-R": "replaceAll", "Ctrl-G": "findNext", "Shift-Ctrl-G": "findPrev", "Shift-Ctrl-F": "replace", "Shift-Ctrl-R": "replaceAll",
"Ctrl-[": "indentLess", "Ctrl-]": "indentMore",
fallthrough: "basic" fallthrough: "basic"
}; };
keyMap.macDefault = { keyMap.macDefault = {
@ -1945,6 +2007,7 @@ var CodeMirror = (function() {
"Alt-Right": "goWordRight", "Cmd-Left": "goLineStart", "Cmd-Right": "goLineEnd", "Alt-Backspace": "delWordLeft", "Alt-Right": "goWordRight", "Cmd-Left": "goLineStart", "Cmd-Right": "goLineEnd", "Alt-Backspace": "delWordLeft",
"Ctrl-Alt-Backspace": "delWordRight", "Alt-Delete": "delWordRight", "Cmd-S": "save", "Cmd-F": "find", "Ctrl-Alt-Backspace": "delWordRight", "Alt-Delete": "delWordRight", "Cmd-S": "save", "Cmd-F": "find",
"Cmd-G": "findNext", "Shift-Cmd-G": "findPrev", "Cmd-Alt-F": "replace", "Shift-Cmd-Alt-F": "replaceAll", "Cmd-G": "findNext", "Shift-Cmd-G": "findPrev", "Cmd-Alt-F": "replace", "Shift-Cmd-Alt-F": "replaceAll",
"Cmd-[": "indentLess", "Cmd-]": "indentMore",
fallthrough: ["basic", "emacsy"] fallthrough: ["basic", "emacsy"]
}; };
keyMap["default"] = mac ? keyMap.macDefault : keyMap.pcDefault; keyMap["default"] = mac ? keyMap.macDefault : keyMap.pcDefault;
@ -1987,6 +2050,8 @@ var CodeMirror = (function() {
options.value = textarea.value; options.value = textarea.value;
if (!options.tabindex && textarea.tabindex) if (!options.tabindex && textarea.tabindex)
options.tabindex = textarea.tabindex; options.tabindex = textarea.tabindex;
if (options.autofocus == null && textarea.getAttribute("autofocus") != null)
options.autofocus = true;
function save() {textarea.value = instance.getValue();} function save() {textarea.value = instance.getValue();}
if (textarea.form) { if (textarea.form) {
@ -2098,34 +2163,34 @@ var CodeMirror = (function() {
}; };
CodeMirror.StringStream = StringStream; CodeMirror.StringStream = StringStream;
function MarkedText(from, to, className, set) { function MarkedText(from, to, className, marker) {
this.from = from; this.to = to; this.style = className; this.set = set; this.from = from; this.to = to; this.style = className; this.marker = marker;
} }
MarkedText.prototype = { MarkedText.prototype = {
attach: function(line) { this.set.push(line); }, attach: function(line) { this.marker.set.push(line); },
detach: function(line) { detach: function(line) {
var ix = indexOf(this.set, line); var ix = indexOf(this.marker.set, line);
if (ix > -1) this.set.splice(ix, 1); if (ix > -1) this.marker.set.splice(ix, 1);
}, },
split: function(pos, lenBefore) { split: function(pos, lenBefore) {
if (this.to <= pos && this.to != null) return null; if (this.to <= pos && this.to != null) return null;
var from = this.from < pos || this.from == null ? null : this.from - pos + lenBefore; var from = this.from < pos || this.from == null ? null : this.from - pos + lenBefore;
var to = this.to == null ? null : this.to - pos + lenBefore; var to = this.to == null ? null : this.to - pos + lenBefore;
return new MarkedText(from, to, this.style, this.set); return new MarkedText(from, to, this.style, this.marker);
}, },
dup: function() { return new MarkedText(null, null, this.style, this.set); }, dup: function() { return new MarkedText(null, null, this.style, this.marker); },
clipTo: function(fromOpen, from, toOpen, to, diff) { clipTo: function(fromOpen, from, toOpen, to, diff) {
if (this.from != null && this.from >= from)
this.from = Math.max(to, this.from) + diff;
if (this.to != null && this.to > from)
this.to = to < this.to ? this.to + diff : from;
if (fromOpen && to > this.from && (to < this.to || this.to == null)) if (fromOpen && to > this.from && (to < this.to || this.to == null))
this.from = null; this.from = null;
else if (this.from != null && this.from >= from)
this.from = Math.max(to, this.from) + diff;
if (toOpen && (from < this.to || this.to == null) && (from > this.from || this.from == null)) if (toOpen && (from < this.to || this.to == null) && (from > this.from || this.from == null))
this.to = null; this.to = null;
else if (this.to != null && this.to > from)
this.to = to < this.to ? this.to + diff : from;
}, },
isDead: function() { return this.from != null && this.to != null && this.from >= this.to; }, isDead: function() { return this.from != null && this.to != null && this.from >= this.to; },
sameSet: function(x) { return this.set == x.set; } sameSet: function(x) { return this.marker == x.marker; }
}; };
function Bookmark(pos) { function Bookmark(pos) {
@ -2168,7 +2233,7 @@ var CodeMirror = (function() {
this.styles = styles || [text, null]; this.styles = styles || [text, null];
this.text = text; this.text = text;
this.height = 1; this.height = 1;
this.marked = this.gutterMarker = this.className = this.handlers = null; this.marked = this.gutterMarker = this.className = this.bgClassName = this.handlers = null;
this.stateAfter = this.parent = this.hidden = null; this.stateAfter = this.parent = this.hidden = null;
} }
Line.inheritMarks = function(text, orig) { Line.inheritMarks = function(text, orig) {
@ -2214,6 +2279,7 @@ var CodeMirror = (function() {
if (newmark) { if (newmark) {
if (!taken.marked) taken.marked = []; if (!taken.marked) taken.marked = [];
taken.marked.push(newmark); newmark.attach(taken); taken.marked.push(newmark); newmark.attach(taken);
if (newmark == mark) mk.splice(i--, 1);
} }
} }
} }
@ -2637,10 +2703,10 @@ var CodeMirror = (function() {
if (start < last.start) { if (start < last.start) {
for (var i = last.start - start - 1; i >= 0; --i) for (var i = last.start - start - 1; i >= 0; --i)
last.old.unshift(old[i]); last.old.unshift(old[i]);
last.added += last.start - start; oldoff = Math.min(0, added - old.length);
last.added += last.start - start + oldoff;
last.start = start; last.start = start;
} } else if (last.start < start) {
else if (last.start < start) {
oldoff = start - last.start; oldoff = start - last.start;
added += oldoff; added += oldoff;
} }
@ -2707,19 +2773,21 @@ var CodeMirror = (function() {
var Pass = CodeMirror.Pass = {toString: function(){return "CodeMirror.Pass";}}; var Pass = CodeMirror.Pass = {toString: function(){return "CodeMirror.Pass";}};
// Detect drag-and-drop
var dragAndDrop = function() {
// IE8 has ondragstart and ondrop properties, but doesn't seem to
// actually support ondragstart the way it's supposed to work.
if (/MSIE [1-8]\b/.test(navigator.userAgent)) return false;
var div = document.createElement('div');
return "draggable" in div;
}();
var gecko = /gecko\/\d{7}/i.test(navigator.userAgent); var gecko = /gecko\/\d{7}/i.test(navigator.userAgent);
var ie = /MSIE \d/.test(navigator.userAgent); var ie = /MSIE \d/.test(navigator.userAgent);
var ie_lt9 = /MSIE [1-8]\b/.test(navigator.userAgent); var ie_lt9 = /MSIE [1-8]\b/.test(navigator.userAgent);
var webkit = /WebKit\//.test(navigator.userAgent); var webkit = /WebKit\//.test(navigator.userAgent);
var chrome = /Chrome\//.test(navigator.userAgent);
var khtml = /KHTML\//.test(navigator.userAgent);
// Detect drag-and-drop
var dragAndDrop = function() {
// There is *some* kind of drag-and-drop support in IE6-8, but I
// couldn't get it to work yet.
if (ie_lt9) return false;
var div = document.createElement('div');
return "draggable" in div || "dragDrop" in div;
}();
var lineSep = "\n"; var lineSep = "\n";
// Feature-detect whether newlines in textareas are converted to \r\n // Feature-detect whether newlines in textareas are converted to \r\n
@ -2873,7 +2941,7 @@ var CodeMirror = (function() {
var keyNames = {3: "Enter", 8: "Backspace", 9: "Tab", 13: "Enter", 16: "Shift", 17: "Ctrl", 18: "Alt", var keyNames = {3: "Enter", 8: "Backspace", 9: "Tab", 13: "Enter", 16: "Shift", 17: "Ctrl", 18: "Alt",
19: "Pause", 20: "CapsLock", 27: "Esc", 32: "Space", 33: "PageUp", 34: "PageDown", 35: "End", 19: "Pause", 20: "CapsLock", 27: "Esc", 32: "Space", 33: "PageUp", 34: "PageDown", 35: "End",
36: "Home", 37: "Left", 38: "Up", 39: "Right", 40: "Down", 44: "PrintScrn", 45: "Insert", 36: "Home", 37: "Left", 38: "Up", 39: "Right", 40: "Down", 44: "PrintScrn", 45: "Insert",
46: "Delete", 59: ";", 91: "Mod", 92: "Mod", 93: "Mod", 186: ";", 187: "=", 188: ",", 46: "Delete", 59: ";", 91: "Mod", 92: "Mod", 93: "Mod", 127: "Delete", 186: ";", 187: "=", 188: ",",
189: "-", 190: ".", 191: "/", 192: "`", 219: "[", 220: "\\", 221: "]", 222: "'", 63276: "PageUp", 189: "-", 190: ".", 191: "/", 192: "`", 219: "[", 220: "\\", 221: "]", 222: "'", 63276: "PageUp",
63277: "PageDown", 63275: "End", 63273: "Home", 63234: "Left", 63232: "Up", 63235: "Right", 63277: "PageDown", 63275: "End", 63273: "Home", 63234: "Left", 63232: "Up", 63235: "Right",
63233: "Down", 63302: "Insert", 63272: "Delete"}; 63233: "Down", 63302: "Insert", 63272: "Delete"};