cocalc/src / webapp-lib / jupyter / components / codemirror / addon / runmode / runmode-standalone.js
50665 views// CodeMirror, copyright (c) by Marijn Haverbeke and others1// Distributed under an MIT license: http://codemirror.net/LICENSE23window.CodeMirror = {};45(function() {6"use strict";78function splitLines(string){ return string.split(/\r?\n|\r/); };910function StringStream(string) {11this.pos = this.start = 0;12this.string = string;13this.lineStart = 0;14}15StringStream.prototype = {16eol: function() {return this.pos >= this.string.length;},17sol: function() {return this.pos == 0;},18peek: function() {return this.string.charAt(this.pos) || null;},19next: function() {20if (this.pos < this.string.length)21return this.string.charAt(this.pos++);22},23eat: function(match) {24var ch = this.string.charAt(this.pos);25if (typeof match == "string") var ok = ch == match;26else var ok = ch && (match.test ? match.test(ch) : match(ch));27if (ok) {++this.pos; return ch;}28},29eatWhile: function(match) {30var start = this.pos;31while (this.eat(match)){}32return this.pos > start;33},34eatSpace: function() {35var start = this.pos;36while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;37return this.pos > start;38},39skipToEnd: function() {this.pos = this.string.length;},40skipTo: function(ch) {41var found = this.string.indexOf(ch, this.pos);42if (found > -1) {this.pos = found; return true;}43},44backUp: function(n) {this.pos -= n;},45column: function() {return this.start - this.lineStart;},46indentation: function() {return 0;},47match: function(pattern, consume, caseInsensitive) {48if (typeof pattern == "string") {49var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};50var substr = this.string.substr(this.pos, pattern.length);51if (cased(substr) == cased(pattern)) {52if (consume !== false) this.pos += pattern.length;53return true;54}55} else {56var match = this.string.slice(this.pos).match(pattern);57if (match && match.index > 0) return null;58if (match && consume !== false) this.pos += match[0].length;59return match;60}61},62current: function(){return this.string.slice(this.start, this.pos);},63hideFirstChars: function(n, inner) {64this.lineStart += n;65try { return inner(); }66finally { this.lineStart -= n; }67}68};69CodeMirror.StringStream = StringStream;7071CodeMirror.startState = function (mode, a1, a2) {72return mode.startState ? mode.startState(a1, a2) : true;73};7475var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};76CodeMirror.defineMode = function (name, mode) {77if (arguments.length > 2)78mode.dependencies = Array.prototype.slice.call(arguments, 2);79modes[name] = mode;80};81CodeMirror.defineMIME = function (mime, spec) { mimeModes[mime] = spec; };82CodeMirror.resolveMode = function(spec) {83if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {84spec = mimeModes[spec];85} else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {86spec = mimeModes[spec.name];87}88if (typeof spec == "string") return {name: spec};89else return spec || {name: "null"};90};91CodeMirror.getMode = function (options, spec) {92spec = CodeMirror.resolveMode(spec);93var mfactory = modes[spec.name];94if (!mfactory) throw new Error("Unknown mode: " + spec);95return mfactory(options, spec);96};97CodeMirror.registerHelper = CodeMirror.registerGlobalHelper = Math.min;98CodeMirror.defineMode("null", function() {99return {token: function(stream) {stream.skipToEnd();}};100});101CodeMirror.defineMIME("text/plain", "null");102103CodeMirror.runMode = function (string, modespec, callback, options) {104var mode = CodeMirror.getMode({ indentUnit: 2 }, modespec);105106if (callback.nodeType == 1) {107var tabSize = (options && options.tabSize) || 4;108var node = callback, col = 0;109node.innerHTML = "";110callback = function (text, style) {111if (text == "\n") {112node.appendChild(document.createElement("br"));113col = 0;114return;115}116var content = "";117// replace tabs118for (var pos = 0; ;) {119var idx = text.indexOf("\t", pos);120if (idx == -1) {121content += text.slice(pos);122col += text.length - pos;123break;124} else {125col += idx - pos;126content += text.slice(pos, idx);127var size = tabSize - col % tabSize;128col += size;129for (var i = 0; i < size; ++i) content += " ";130pos = idx + 1;131}132}133134if (style) {135var sp = node.appendChild(document.createElement("span"));136sp.className = "cm-" + style.replace(/ +/g, " cm-");137sp.appendChild(document.createTextNode(content));138} else {139node.appendChild(document.createTextNode(content));140}141};142}143144var lines = splitLines(string), state = (options && options.state) || CodeMirror.startState(mode);145for (var i = 0, e = lines.length; i < e; ++i) {146if (i) callback("\n");147var stream = new CodeMirror.StringStream(lines[i]);148if (!stream.string && mode.blankLine) mode.blankLine(state);149while (!stream.eol()) {150var style = mode.token(stream, state);151callback(stream.current(), style, i, stream.start, state);152stream.start = stream.pos;153}154}155};156})();157158159