2
0

changemode.html 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>CodeMirror 2: Mode-Changing Demo</title>
  5. <link rel="stylesheet" href="../lib/codemirror.css">
  6. <script src="../lib/codemirror.js"></script>
  7. <link rel="stylesheet" href="../theme/default.css">
  8. <script src="../mode/javascript/javascript.js"></script>
  9. <script src="../mode/scheme/scheme.js"></script>
  10. <link rel="stylesheet" href="../css/docs.css">
  11. <style type="text/css">
  12. .CodeMirror {border: 1px solid black;}
  13. </style>
  14. </head>
  15. <body>
  16. <h1>CodeMirror 2: Mode-Changing demo</h1>
  17. <form><textarea id="code" name="code">
  18. ;; If there is Scheme code in here, the editor will be in Scheme mode.
  19. ;; If you put in JS instead, it'll switch to JS mode.
  20. (define (double x)
  21. (* x x))
  22. </textarea></form>
  23. <p>On changes to the content of the above editor, a (crude) script
  24. tries to auto-detect the language used, and switches the editor to
  25. either JavaScript or Scheme mode based on that.</p>
  26. <script>
  27. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  28. mode: "scheme",
  29. lineNumbers: true,
  30. matchBrackets: true,
  31. tabMode: "indent",
  32. onChange: function() {
  33. clearTimeout(pending);
  34. setTimeout(update, 400);
  35. }
  36. });
  37. var pending;
  38. function looksLikeScheme(code) {
  39. return !/^\s*\(\s*function\b/.test(code) && /^\s*[;\(]/.test(code);
  40. }
  41. function update() {
  42. editor.setOption("mode", looksLikeScheme(editor.getValue()) ? "scheme" : "javascript");
  43. }
  44. </script>
  45. </body>
  46. </html>