1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 | <!doctype html> <html> <head> <title>CodeMirror: User Manual</title> <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Droid+Sans|Droid+Sans:bold"/> <link rel="stylesheet" type="text/css" href="docs.css"/> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <style>dl dl {margin: 0;}</style> </head> <body> <h1><span class="logo-braces">{ }</span> <a href="http://codemirror.net/">CodeMirror</a></h1> <pre class="grey"> <img src="baboon.png" class="logo" alt="logo"/>/* User manual and reference guide */ </pre> <div class="clear"><div class="leftbig blk"> <h2 id="overview">Overview</h2> <p>CodeMirror is a code-editor component that can be embedded in Web pages. The code library provides <em>only</em> the editor component, no accompanying buttons, auto-completion, or other IDE functionality. It does provide a rich API on top of which such functionality can be straightforwardly implemented. See the <a href="#addons">add-ons</a> included in the distribution, and the <a href="http://www.octolabs.com/javascripts/codemirror-ui/">CodeMirror UI</a> project, for reusable implementations of extra features.</p> <p>CodeMirror works with language-specific modes. Modes are JavaScript programs that help color (and optionally indent) text written in a given language. The distribution comes with a few modes (see the <code>mode/</code> directory), and it isn't hard to <a href="#modeapi">write new ones</a> for other languages.</p> <h2 id="usage">Basic Usage</h2> <p>The easiest way to use CodeMirror is to simply load the script and style sheet found under <code>lib/</code> in the distribution, plus a mode script from one of the <code>mode/</code> directories and a theme stylesheet from <code>theme/</code>. (See also <a href="compress.html">the compression helper</a>.) For example:</p> <pre><script src="lib/codemirror.js"></script> <link rel="stylesheet" href="../lib/codemirror.css"> <script src="mode/javascript/javascript.js"></script></pre> <p>Having done this, an editor instance can be created like this:</p> <pre>var myCodeMirror = CodeMirror(document.body);</pre> <p>The editor will be appended to the document body, will start empty, and will use the mode that we loaded. To have more control over the new editor, a configuration object can be passed to <code>CodeMirror</code> as a second argument:</p> <pre>var myCodeMirror = CodeMirror(document.body, { value: "function myScript(){return 100;}\n", mode: "javascript" });</pre> <p>This will initialize the editor with a piece of code already in it, and explicitly tell it to use the JavaScript mode (which is useful when multiple modes are loaded). See <a href="#config">below</a> for a full discussion of the configuration options that CodeMirror accepts.</p> <p>In cases where you don't want to append the editor to an element, and need more control over the way it is inserted, the first argument to the <code>CodeMirror</code> function can also be a function that, when given a DOM element, inserts it into the document somewhere. This could be used to, for example, replace a textarea with a real editor:</p> <pre>var myCodeMirror = CodeMirror(function(elt) { myTextArea.parentNode.replaceChild(elt, myTextArea); }, {value: myTextArea.value});</pre> <p>However, for this use case, which is a common way to use CodeMirror, the library provides a much more powerful shortcut:</p> <pre>var myCodeMirror = CodeMirror.fromTextArea(myTextArea);</pre> <p>This will, among other things, ensure that the textarea's value is updated when the form (if it is part of a form) is submitted. See the <a href="#fromTextArea">API reference</a> for a full description of this method.</p> <h2 id="config">Configuration</h2> <p>Both the <code>CodeMirror</code> function and its <code>fromTextArea</code> method take as second (optional) argument an object containing configuration options. Any option not supplied like this will be taken from <code>CodeMirror.defaults</code>, an object containing the default options. You can update this object to change the defaults on your page.</p> <p>Options are not checked in any way, so setting bogus option values is bound to lead to odd errors.</p> <p>These are the supported options:</p> <dl> <dt id="option_value"><code>value (string)</code></dt> <dd>The starting value of the editor.</dd> <dt id="option_mode"><code>mode (string or object)</code></dt> <dd>The mode to use. When not given, this will default to the first mode that was loaded. It may be a string, which either simply names the mode or is a <a href="http://en.wikipedia.org/wiki/MIME">MIME</a> type associated with the mode. Alternatively, it may be an object containing configuration options for the mode, with a <code>name</code> property that names the mode (for example <code>{name: "javascript", json: true}</code>). The demo pages for each mode contain information about what configuration parameters the mode supports. You can ask CodeMirror which modes and MIME types are loaded with the <code>CodeMirror.listModes</code> and <code>CodeMirror.listMIMEs</code> functions.</dd> <dt id="option_theme"><code>theme (string)</code></dt> <dd>The theme to style the editor with. You must make sure the CSS file defining the corresponding <code>.cm-s-[name]</code> styles is loaded (see the <a href="../theme/"><code>theme</code></a> directory in the distribution). The default is <code>"default"</code>, for which colors are included in <code>codemirror.css</code>. It is possible to use multiple theming classes at once—for example <code>"foo bar"</code> will assign both the <code>cm-s-foo</code> and the <code>cm-s-bar</code> classes to the editor.</dd> <dt id="option_indentUnit"><code>indentUnit (integer)</code></dt> <dd>How many spaces a block (whatever that means in the edited language) should be indented. The default is 2.</dd> <dt id="option_tabSize"><code>tabSize (integer)</code></dt> <dd>The width of a tab character. Defaults to 4.</dd> <dt id="option_indentWithTabs"><code>indentWithTabs (boolean)</code></dt> <dd>Whether, when indenting, the first N*<code>tabSize</code> spaces should be replaced by N tabs. Default is false.</dd> <dt id="option_electricChars"><code>electricChars (boolean)</code></dt> <dd>Configures whether the editor should re-indent the current line when a character is typed that might change its proper indentation (only works if the mode supports indentation). Default is true.</dd> <dt id="option_keyMap"><code>keyMap (string)</code></dt> <dd>Configures the keymap to use. The default is <code>"default"</code>, which is the only keymap defined in <code>codemirror.js</code> itself. Extra keymaps are found in the <a href="../keymap/"><code>keymap</code></a> directory.</dd> <dt id="option_extraKeys"><code>extraKeys (object)</code></dt> <dd>Can be used to specify extra keybindings for the editor. When given, should be an object with property names like <code>Ctrl-A</code>, <code>Home</code>, and <code>Ctrl-Alt-Left</code>. See the <code>CodeMirror.keyNames</code> object for the names of all the keys. The values in this object can either be functions, which will be called with the CodeMirror instance when the key is pressed, or strings, which should name commands defined in <code>CodeMirror.commands</code> (not documented properly, but looking at the source and the definition of the built-in keymaps, they should be rather obvious).</dd> <dt id="option_lineWrapping"><code>lineWrapping (boolean)</code></dt> <dd>Whether CodeMirror should scroll or wrap for long lines. Defaults to <code>false</code> (scroll).</dd> <dt id="option_lineNumbers"><code>lineNumbers (boolean)</code></dt> <dd>Whether to show line numbers to the left of the editor.</dd> <dt id="option_firstLineNumber"><code>firstLineNumber (integer)</code></dt> <dd>At which number to start counting lines. Default is 1.</dd> <dt id="option_gutter"><code>gutter (boolean)</code></dt> <dd>Can be used to force a 'gutter' (empty space on the left of the editor) to be shown even when no line numbers are active. This is useful for setting <a href="#setMarker">markers</a>.</dd> <dt id="option_fixedGutter"><code>fixedGutter (boolean)</code></dt> <dd>When enabled (off by default), this will make the gutter stay visible when the document is scrolled horizontally.</dd> <dt id="option_readOnly"><code>readOnly (boolean)</code></dt> <dd>This disables editing of the editor content by the user.</dd> <dt id="option_onChange"><code>onChange (function)</code></dt> <dd>When given, this function will be called every time the content of the editor is changed. It will be given the editor instance as first argument, and an <code>{from, to, newText, next}</code> object containing information about the changes that occurred as second argument. <code>from</code> and <code>to</code> are the positions (in the pre-change coordinate system) where the change started and ended. <code>newText</code> is an array of strings representing the text that replaced the changed range (split by line). If multiple changes happened during a single operation, the object will have a <code>next</code> property pointing to another change object (which may point to another, etc).</dd> <dt id="option_onCursorActivity"><code>onCursorActivity (function)</code></dt> <dd>Will be called when the cursor or selection moves, or any change is made to the editor content.</dd> <dt id="option_onGutterClick"><code>onGutterClick (function)</code></dt> <dd>When given, will be called whenever the editor gutter (the line-number area) is clicked. Will be given the editor instance as first argument, the (zero-based) number of the line that was clicked as second argument, and the raw <code>mousedown</code> event object as third argument.</dd> <dt id="option_onFocus"><code>onFocus, onBlur (function)</code></dt> <dd>The given functions will be called whenever the editor is focused or unfocused.</dd> <dt id="option_onScroll"><code>onScroll (function)</code></dt> <dd>When given, will be called whenever the editor is scrolled.</dd> <dt id="option_onHighlightComplete"><code>onHighlightComplete (function)</code></dt> <dd>Whenever the editor's content has been fully highlighted, this function (if given) will be called. It'll be given a single argument, the editor instance.</dd> <dt id="option_onUpdate"><code>onUpdate (function)</ |