1
0

changemode.html 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  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">
  11. <style type="text/css">
  12. .CodeMirror {border: 1px solid black;}
  13. </style>
  14. </head>
  15. <body>
  16. <h1>CodeMirror: 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. tabMode: "indent"
  31. });
  32. editor.on("change", function() {
  33. clearTimeout(pending);
  34. setTimeout(update, 400);
  35. });
  36. var pending;
  37. function looksLikeScheme(code) {
  38. return !/^\s*\(\s*function\b/.test(code) && /^\s*[;\(]/.test(code);
  39. }
  40. function update() {
  41. editor.setOption("mode", looksLikeScheme(editor.getValue()) ? "scheme" : "javascript");
  42. }
  43. </script>
  44. </body>
  45. </html>