CMIContent Marketing InstituteContent directory

News · General · Archive

CodeMirror {border: 1px solid black;} CodeMirror Home Manual Code

Home News

.CodeMirror {border: 1px solid black;}

CodeMirror

Home Manual Code

Mode-Changing

Mode-Changing Demo

;; If there is Scheme code in here, the editor will be in Scheme mode. ;; If you put in JS instead, it'll switch to JS mode.

(define (double x) (* x x))

On changes to the content of the above editor, a (crude) script tries to auto-detect the language used, and switches the editor to either JavaScript or Scheme mode based on that.

var editor = CodeMirror.fromTextArea(document.getElementById("code"), { mode: "scheme", lineNumbers: true }); var pending; editor.on("change", function() { clearTimeout(pending); pending = setTimeout(update, 400); }); function looksLikeScheme(code) { return !/^\s*\(\s*function\b/.test(code) && /^\s*[;\(]/.test(code); } function update() { editor.setOption("mode", looksLikeScheme(editor.getValue()) ? "scheme" : "javascript"); }

More in Archive · More in General · More in News