5 <title>CodeMirror: Mode-Changing Demo</title>
6 <link rel="stylesheet" href="../lib/codemirror.css">
7 <script src="../lib/codemirror.js"></script>
8 <script src="../mode/javascript/javascript.js"></script>
9 <script src="../mode/scheme/scheme.js"></script>
10 <link rel="stylesheet" href="../doc/docs.css">
12 <style type="text/css">
13 .CodeMirror {border: 1px solid black;}
17 <h1>CodeMirror: Mode-Changing demo</h1>
19 <form><textarea id="code" name="code">
20 ;; If there is Scheme code in here, the editor will be in Scheme mode.
21 ;; If you put in JS instead, it'll switch to JS mode.
27 <p>On changes to the content of the above editor, a (crude) script
28 tries to auto-detect the language used, and switches the editor to
29 either JavaScript or Scheme mode based on that.</p>
32 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
37 editor.on("change", function() {
38 clearTimeout(pending);
39 setTimeout(update, 400);
42 function looksLikeScheme(code) {
43 return !/^\s*\(\s*function\b/.test(code) && /^\s*[;\(]/.test(code);
46 editor.setOption("mode", looksLikeScheme(editor.getValue()) ? "scheme" : "javascript");