5 <title>CodeMirror: User Manual</title>
6 <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Droid+Sans|Droid+Sans:bold">
7 <link rel="stylesheet" type="text/css" href="docs.css">
8 <style>dl dl {margin: 0;}</style>
9 <script src="../lib/codemirror.js"></script>
10 <link rel="stylesheet" href="../lib/codemirror.css">
11 <script src="../addon/runmode/runmode.js"></script>
12 <script src="../addon/runmode/colorize.js"></script>
13 <script src="../mode/javascript/javascript.js"></script>
14 <script src="../mode/xml/xml.js"></script>
15 <script src="../mode/css/css.js"></script>
16 <script src="../mode/htmlmixed/htmlmixed.js"></script>
18 dt { text-indent: -1.5em; padding-left: 1.5em; }
23 <h1><span class="logo-braces">{ }</span> <a href="http://codemirror.net/">CodeMirror</a></h1>
26 <img src="baboon.png" class="logo" alt="logo">
33 <div class="clear"><div class="leftbig blk">
35 <h2 id="overview">Overview</h2>
37 <p>CodeMirror is a code-editor component that can be embedded in
38 Web pages. The core library provides <em>only</em> the editor
39 component, no accompanying buttons, auto-completion, or other IDE
40 functionality. It does provide a rich API on top of which such
41 functionality can be straightforwardly implemented. See
42 the <a href="#addons">addons</a> included in the distribution,
43 and the <a href="https://github.com/marijnh/CodeMirror/wiki/CodeMirror-addons">list
44 of externally hosted addons</a>, for reusable
45 implementations of extra features.</p>
47 <p>CodeMirror works with language-specific modes. Modes are
48 JavaScript programs that help color (and optionally indent) text
49 written in a given language. The distribution comes with a number
50 of modes (see the <a href="../mode/"><code>mode/</code></a>
51 directory), and it isn't hard to <a href="#modeapi">write new
52 ones</a> for other languages.</p>
54 <h2 id="usage">Basic Usage</h2>
56 <p>The easiest way to use CodeMirror is to simply load the script
57 and style sheet found under <code>lib/</code> in the distribution,
58 plus a mode script from one of the <code>mode/</code> directories.
59 (See <a href="compress.html">the compression helper</a> for an
60 easy way to combine scripts.) For example:</p>
62 <pre data-lang="text/html"><script src="lib/codemirror.js"></script>
63 <link rel="stylesheet" href="../lib/codemirror.css">
64 <script src="mode/javascript/javascript.js"></script></pre>
66 <p>Having done this, an editor instance can be created like
69 <pre data-lang="javascript">var myCodeMirror = CodeMirror(document.body);</pre>
71 <p>The editor will be appended to the document body, will start
72 empty, and will use the mode that we loaded. To have more control
73 over the new editor, a configuration object can be passed
74 to <a href="#CodeMirror"><code>CodeMirror</code></a> as a second
77 <pre data-lang="javascript">var myCodeMirror = CodeMirror(document.body, {
78 value: "function myScript(){return 100;}\n",
82 <p>This will initialize the editor with a piece of code already in
83 it, and explicitly tell it to use the JavaScript mode (which is
84 useful when multiple modes are loaded).
85 See <a href="#config">below</a> for a full discussion of the
86 configuration options that CodeMirror accepts.</p>
88 <p>In cases where you don't want to append the editor to an
89 element, and need more control over the way it is inserted, the
90 first argument to the <code>CodeMirror</code> function can also
91 be a function that, when given a DOM element, inserts it into the
92 document somewhere. This could be used to, for example, replace a
93 textarea with a real editor:</p>
95 <pre data-lang="javascript">var myCodeMirror = CodeMirror(function(elt) {
96 myTextArea.parentNode.replaceChild(elt, myTextArea);
97 }, {value: myTextArea.value});</pre>
99 <p>However, for this use case, which is a common way to use
100 CodeMirror, the library provides a much more powerful
103 <pre data-lang="javascript">var myCodeMirror = CodeMirror.fromTextArea(myTextArea);</pre>
105 <p>This will, among other things, ensure that the textarea's value
106 is updated with the editor's contents when the form (if it is part
107 of a form) is submitted. See the <a href="#fromTextArea">API
108 reference</a> for a full description of this method.</p>
110 <h2 id="config">Configuration</h2>
112 <p>Both the <a href="#CodeMirror"><code>CodeMirror</code></a>
113 function and its <code>fromTextArea</code> method take as second
114 (optional) argument an object containing configuration options.
115 Any option not supplied like this will be taken
116 from <a href="#defaults"><code>CodeMirror.defaults</code></a>, an
117 object containing the default options. You can update this object
118 to change the defaults on your page.</p>
120 <p>Options are not checked in any way, so setting bogus option
121 values is bound to lead to odd errors.</p>
123 <p>These are the supported options:</p>
126 <dt id="option_value"><code><strong>value</strong>: string|CodeMirror.Doc</code></dt>
127 <dd>The starting value of the editor. Can be a string, or
128 a <a href="#api_doc">document object</a>.</dd>
130 <dt id="option_mode"><code><strong>mode</strong>: string|object</code></dt>
131 <dd>The mode to use. When not given, this will default to the
132 first mode that was loaded. It may be a string, which either
133 simply names the mode or is
134 a <a href="http://en.wikipedia.org/wiki/MIME">MIME</a> type
135 associated with the mode. Alternatively, it may be an object
136 containing configuration options for the mode, with
137 a <code>name</code> property that names the mode (for
138 example <code>{name: "javascript", json: true}</code>). The demo
139 pages for each mode contain information about what configuration
140 parameters the mode supports. You can ask CodeMirror which modes
141 and MIME types have been defined by inspecting
142 the <code>CodeMirror.modes</code>
143 and <code>CodeMirror.mimeModes</code> objects. The first maps
144 mode names to their constructors, and the second maps MIME types
147 <dt id="option_theme"><code><strong>theme</strong>: string</code></dt>
148 <dd>The theme to style the editor with. You must make sure the
149 CSS file defining the corresponding <code>.cm-s-[name]</code>
150 styles is loaded (see
151 the <a href="../theme/"><code>theme</code></a> directory in the
152 distribution). The default is <code>"default"</code>, for which
153 colors are included in <code>codemirror.css</code>. It is
154 possible to use multiple theming classes at once—for
155 example <code>"foo bar"</code> will assign both
156 the <code>cm-s-foo</code> and the <code>cm-s-bar</code> classes
159 <dt id="option_indentUnit"><code><strong>indentUnit</strong>: integer</code></dt>
160 <dd>How many spaces a block (whatever that means in the edited
161 language) should be indented. The default is 2.</dd>
163 <dt id="option_smartIndent"><code><strong>smartIndent</strong>: boolean</code></dt>
164 <dd>Whether to use the context-sensitive indentation that the
165 mode provides (or just indent the same as the line before).
166 Defaults to true.</dd>
168 <dt id="option_tabSize"><code><strong>tabSize</strong>: integer</code></dt>
169 <dd>The width of a tab character. Defaults to 4.</dd>
171 <dt id="option_indentWithTabs"><code><strong>indentWithTabs</strong>: boolean</code></dt>
172 <dd>Whether, when indenting, the first N*<code>tabSize</code>
173 spaces should be replaced by N tabs. Default is false.</dd>
175 <dt id="option_electricChars"><code><strong>electricChars</strong>: boolean</code></dt>
176 <dd>Configures whether the editor should re-indent the current
177 line when a character is typed that might change its proper
178 indentation (only works if the mode supports indentation).
179 Default is true.</dd>
181 <dt id="option_rtlMoveVisually"><code><strong>rtlMoveVisually</strong>: boolean</code></dt>
182 <dd>Determines whether horizontal cursor movement through
183 right-to-left (Arabic, Hebrew) text is visual (pressing the left
184 arrow moves the cursor left) or logical (pressing the left arrow
185 moves to the next lower index in the string, which is visually
186 right in right-to-left text). The default is <code>false</code>
187 on Windows, and <code>true</code> on other platforms.</dd>
189 <dt id="option_keyMap"><code><strong>keyMap</strong>: string</code></dt>
190 <dd>Configures the keymap to use. The default
191 is <code>"default"</code>, which is the only keymap defined
192 in <code>codemirror.js</code> itself. Extra keymaps are found in
193 the <a href="../keymap/"><code>keymap</code></a> directory. See
194 the <a href="#keymaps">section on keymaps</a> for more
197 <dt id="option_extraKeys"><code><strong>extraKeys</strong>: object</code></dt>
198 <dd>Can be used to specify extra keybindings for the editor,
199 alongside the ones defined
200 by <a href="#option_keyMap"><code>keyMap</code></a>. Should be
201 either null, or a valid <a href="#keymaps">keymap</a> value.</dd>
203 <dt id="option_lineWrapping"><code><strong>lineWrapping</strong>: boolean</code></dt>
204 <dd>Whether CodeMirror should scroll or wrap for long lines.
205 Defaults to <code>false</code> (scroll).</dd>
207 <dt id="option_lineNumbers"><code><strong>lineNumbers</strong>: boolean</code></dt>
208 <dd>Whether to show line numbers to the left of the editor.</dd>
210 <dt id="option_firstLineNumber"><code><strong>firstLineNumber</strong>: integer</code></dt>
211 <dd>At which number to start counting lines. Default is 1.</dd>
213 <dt id="option_lineNumberFormatter"><code><strong>lineNumberFormatter</strong>: function(line: integer) → string</code></dt>
214 <dd>A function used to format line numbers. The function is
215 passed the line number, and should return a string that will be
216 shown in the gutter.</dd>
218 <dt id="option_gutters"><code><strong>gutters</strong>: array<string></code></dt>
219 <dd>Can be used to add extra gutters (beyond or instead of the
220 line number gutter). Should be an array of CSS class names, each
221 of which defines a <code>width</code> (and optionally a
222 background), and which will be used to draw the background of
223 the gutters. <em>May</em> include
224 the <code>CodeMirror-linenumbers</code> class, in order to
225 explicitly set the position of the line number gutter (it will
226 default to be to the right of all other gutters). These class
227 names are the keys passed
228 to <a href="#setGutterMarker"><code>setGutterMarker</code></a>.</dd>
230 <dt id="option_fixedGutter"><code><strong>fixedGutter</strong>: boolean</code></dt>
231 <dd>Determines whether the gutter scrolls along with the content
232 horizontally (false) or whether it stays fixed during horizontal
233 scrolling (true, the default).</dd>
235 <dt id="option_coverGutterNextToScrollbar"><code><strong>coverGutterNextToScrollbar</strong>: boolean</code></dt>
236 <dd>When <a href="#option_fixedGutter"><code>fixedGutter</code></a>
237 is on, and there is a horizontal scrollbar, by default the
238 gutter will be visible to the left of this scrollbar. If this
239 option is set to true, it will be covered by an element with
240 class <code>CodeMirror-gutter-filler</code>.</dd>
242 <dt id="option_readOnly"><code><strong>readOnly</strong>: boolean|string</code></dt>
243 <dd>This disables editing of the editor content by the user. If
244 the special value <code>"nocursor"</code> is given (instead of
245 simply <code>true</code>), focusing of the editor is also
248 <dt id="option_showCursorWhenSelecting"><code><strong>showCursorWhenSelecting</strong>: boolean</code></dt>
249 <dd>Whether the cursor should be drawn when a selection is
250 active. Defaults to false.</dd>
252 <dt id="option_undoDepth"><code><strong>undoDepth</strong>: integer</code></dt>
253 <dd>The maximum number of undo levels that the editor stores.
256 <dt id="option_historyEventDelay"><code><strong>historyEventDelay</strong>: integer</code></dt>
257 <dd>The period of inactivity (in milliseconds) that will cause a
258 new history event to be started when typing or deleting.
259 Defaults to 500.</dd>
261 <dt id="option_tabindex"><code><strong>tabindex</strong>: integer</code></dt>
262 <dd>The <a href="http://www.w3.org/TR/html401/interact/forms.html#adef-tabindex">tab
263 index</a> to assign to the editor. If not given, no tab index
264 will be assigned.</dd>
266 <dt id="option_autofocus"><code><strong>autofocus</strong>: boolean</code></dt>
267 <dd>Can be used to make CodeMirror focus itself on
268 initialization. Defaults to off.
269 When <a href="#fromTextArea"><code>fromTextArea</code></a> is
270 used, and no explicit value is given for this option, it will be
271 set to true when either the source textarea is focused, or it
272 has an <code>autofocus</code> attribute and no other element is
276 <p>Below this a few more specialized, low-level options are
277 listed. These are only useful in very specific situations, you
278 might want to skip them the first time you read this manual.</p>
281 <dt id="option_dragDrop"><code><strong>dragDrop</strong>: boolean</code></dt>
282 <dd>Controls whether drag-and-drop is enabled. On by default.</dd>
284 <dt id="option_onDragEvent"><code><strong>onDragEvent</strong>: function(instance: CodeMirror, event: Event) → boolean</code></dt>
285 <dd><em>Deprecated! See <a href="#event_dom">these</a> event
286 handlers for the current recommended approach.</em><br>When given,
287 this will be called when the editor is handling
288 a <code>dragenter</code>, <code>dragover</code>,
289 or <code>drop</code> event. It will be passed the editor
290 instance and the event object as arguments. The callback can
291 choose to handle the event itself, in which case it should
292 return <code>true</code> to indicate that CodeMirror should not
293 do anything further.</dd>
295 <dt id="option_onKeyEvent"><code><strong>onKeyEvent</strong>: function(instance: CodeMirror, event: Event) → boolean</code></dt>
296 <dd><em>Deprecated! See <a href="#event_dom">these</a> event
297 handlers for the current recommended approach.</em><br>This
298 provides a rather low-level hook into CodeMirror's key handling.
299 If provided, this function will be called on
300 every <code>keydown</code>, <code>keyup</code>,
301 and <code>keypress</code> event that CodeMirror captures. It
302 will be passed two arguments, the editor instance and the key
303 event. This key event is pretty much the raw key event, except
304 that a <code>stop()</code> method is always added to it. You
305 could feed it to, for example, <code>jQuery.Event</code> to
306 further normalize it.<br>This function can inspect the key
307 event, and handle it if it wants to. It may return true to tell
308 CodeMirror to ignore the event. Be wary that, on some browsers,
309 stopping a <code>keydown</code> does not stop
310 the <code>keypress</code> from firing, whereas on others it
311 does. If you respond to an event, you should probably inspect
312 its <code>type</code> property and only do something when it
313 is <code>keydown</code> (or <code>keypress</code> for actions
314 that need character data).</dd>
316 <dt id="option_cursorBlinkRate"><code><strong>cursorBlinkRate</strong>: number</code></dt>
317 <dd>Half-period in milliseconds used for cursor blinking. The default blink
320 <dt id="option_cursorScrollMargin"><code><strong>cursorScrollMargin</strong>: number</code></dt>
321 <dd>How much extra space to always keep above and below the
322 cursor when approaching the top or bottom of the visible view in
323 a scrollable document. Default is 0.</dd>
325 <dt id="option_cursorHeight"><code><strong>cursorHeight</strong>: number</code></dt>
326 <dd>Determines the height of the cursor. Default is 1, meaning
327 it spans the whole height of the line. For some fonts (and by
328 some tastes) a smaller height (for example <code>0.85</code>),
329 which causes the cursor to not reach all the way to the bottom
330 of the line, looks better</dd>
332 <dt id="option_workTime"><code><strong>workTime</strong>, <strong>workDelay</strong>: number</code></dt>
333 <dd>Highlighting is done by a pseudo background-thread that will
334 work for <code>workTime</code> milliseconds, and then use
335 timeout to sleep for <code>workDelay</code> milliseconds. The
336 defaults are 200 and 300, you can change these options to make
337 the highlighting more or less aggressive.</dd>
339 <dt id="option_workDelay"><code><strong>workDelay</strong>: number</code></dt>
340 <dd>See <a href="#option_workTime"><code>workTime</code></a>.</dd>
342 <dt id="option_pollInterval"><code><strong>pollInterval</strong>: number</code></dt>
343 <dd>Indicates how quickly CodeMirror should poll its input
344 textarea for changes (when focused). Most input is captured by
345 events, but some things, like IME input on some browsers, don't
346 generate events that allow CodeMirror to properly detect it.
347 Thus, it polls. Default is 100 milliseconds.</dd>
349 <dt id="option_flattenSpans"><code><strong>flattenSpans</strong>: boolean</code></dt>
350 <dd>By default, CodeMirror will combine adjacent tokens into a
351 single span if they have the same class. This will result in a
352 simpler DOM tree, and thus perform better. With some kinds of
353 styling (such as rounded corners), this will change the way the
354 document looks. You can set this option to false to disable this
357 <dt id="option_maxHighlightLength"><code><strong>maxHighlightLength</strong>: number</code></dt>
358 <dd>When highlighting long lines, in order to stay responsive,
359 the editor will give up and simply style the rest of the line as
360 plain text when it reaches a certain position. The default is
361 10000. You can set this to <code>Infinity</code> to turn off
364 <dt id="option_viewportMargin"><code><strong>viewportMargin</strong>: integer</code></dt>
365 <dd>Specifies the amount of lines that are rendered above and
366 below the part of the document that's currently scrolled into
367 view. This affects the amount of updates needed when scrolling,
368 and the amount of work that such an update does. You should
369 usually leave it at its default, 10. Can be set
370 to <code>Infinity</code> to make sure the whole document is
371 always rendered, and thus the browser's text search works on it.
372 This <em>will</em> have bad effects on performance of big
376 <h2 id="events">Events</h2>
378 <p>Various CodeMirror-related objects emit events, which allow
379 client code to react to various situations. Handlers for such
380 events can be registed with the <a href="#on"><code>on</code></a>
381 and <a href="#off"><code>off</code></a> methods on the objects
382 that the event fires on. To fire your own events,
383 use <code>CodeMirror.signal(target, name, args...)</code>,
384 where <code>target</code> is a non-DOM-node object.</p>
386 <p>An editor instance fires the following events.
387 The <code>instance</code> argument always refers to the editor
391 <dt id="event_change"><code><strong>"change"</strong> (instance: CodeMirror, changeObj: object)</code></dt>
392 <dd>Fires every time the content of the editor is changed.
393 The <code>changeObj</code> is a <code>{from, to, text, removed,
394 next}</code> object containing information about the changes
395 that occurred as second argument. <code>from</code>
396 and <code>to</code> are the positions (in the pre-change
397 coordinate system) where the change started and ended (for
398 example, it might be <code>{ch:0, line:18}</code> if the
399 position is at the beginning of line #19). <code>text</code> is
400 an array of strings representing the text that replaced the
401 changed range (split by line). <code>removed</code> is the text
402 that used to be between <code>from</code> and <code>to</code>,
403 which is overwritten by this change. If multiple changes
404 happened during a single operation, the object will have
405 a <code>next</code> property pointing to another change object
406 (which may point to another, etc).</dd>
408 <dt id="event_beforeChange"><code><strong>"beforeChange"</strong> (instance: CodeMirror, changeObj: object)</code></dt>
409 <dd>This event is fired before a change is applied, and its
410 handler may choose to modify or cancel the change.
411 The <code>changeObj</code> object
412 has <code>from</code>, <code>to</code>, and <code>text</code>
414 the <a href="#event_change"><code>"change"</code></a> event, but
415 never a <code>next</code> property, since this is fired for each
416 individual change, and not batched per operation. It also has
417 a <code>cancel()</code> method, which can be called to cancel
418 the change, and, <strong>if</strong> the change isn't coming
419 from an undo or redo event, an <code>update(from, to,
420 text)</code> method, which may be used to modify the change.
421 Undo or redo changes can't be modified, because they hold some
422 metainformation for restoring old marked ranges that is only
423 valid for that specific change. All three arguments
424 to <code>update</code> are optional, and can be left off to
425 leave the existing value for that field
426 intact. <strong>Note:</strong> you may not do anything from
427 a <code>"beforeChange"</code> handler that would cause changes
428 to the document or its visualization. Doing so will, since this
429 handler is called directly from the bowels of the CodeMirror
430 implementation, probably cause the editor to become
433 <dt id="event_cursorActivity"><code><strong>"cursorActivity"</strong> (instance: CodeMirror)</code></dt>
434 <dd>Will be fired when the cursor or selection moves, or any
435 change is made to the editor content.</dd>
437 <dt id="event_keyHandled"><code><strong>"keyHandled"</strong> (instance: CodeMirror, name: string, event: Event)</code></dt>
438 <dd>Fired after a key is handled through a
439 keymap. <code>name</code> is the name of the handled key (for
440 example <code>"Ctrl-X"</code> or <code>"'q'"</code>),
441 and <code>event</code> is the DOM <code>keydown</code>
442 or <code>keypress</code> event.</dd>
444 <dt id="event_inputRead"><code><strong>"inputRead"</strong> (instance: CodeMirror, changeObj: object)</code></dt>
445 <dd>Fired whenever new input is read from the hidden textarea
446 (typed or pasted by the user).</dd>
448 <dt id="event_beforeSelectionChange"><code><strong>"beforeSelectionChange"</strong> (instance: CodeMirror, selection: {head, anchor})</code></dt>
449 <dd>This event is fired before the selection is moved. Its
450 handler may modify the resulting selection head and anchor.
451 The <code>selection</code> parameter is an object
452 with <code>head</code> and <code>anchor</code> properties
453 holding <code>{line, ch}</code> objects, which the handler can
454 read and update. Handlers for this event have the same
456 as <a href="#event_beforeChange"><code>"beforeChange"</code></a>
457 handlers — they should not do anything to directly update the
458 state of the editor.</dd>
460 <dt id="event_viewportChange"><code><strong>"viewportChange"</strong> (instance: CodeMirror, from: number, to: number)</code></dt>
461 <dd>Fires whenever the <a href="#getViewport">view port</a> of
462 the editor changes (due to scrolling, editing, or any other
463 factor). The <code>from</code> and <code>to</code> arguments
464 give the new start and end of the viewport.</dd>
466 <dt id="event_gutterClick"><code><strong>"gutterClick"</strong> (instance: CodeMirror, line: integer, gutter: string, clickEvent: Event)</code></dt>
467 <dd>Fires when the editor gutter (the line-number area) is
468 clicked. Will pass the editor instance as first argument, the
469 (zero-based) number of the line that was clicked as second
470 argument, the CSS class of the gutter that was clicked as third
471 argument, and the raw <code>mousedown</code> event object as
472 fourth argument.</dd>
474 <dt id="event_focus"><code><strong>"focus"</strong> (instance: CodeMirror)</code></dt>
475 <dd>Fires whenever the editor is focused.</dd>
477 <dt id="event_blur"><code><strong>"blur"</strong> (instance: CodeMirror)</code></dt>
478 <dd>Fires whenever the editor is unfocused.</dd>
480 <dt id="event_scroll"><code><strong>"scroll"</strong> (instance: CodeMirror)</code></dt>
481 <dd>Fires when the editor is scrolled.</dd>
483 <dt id="event_update"><code><strong>"update"</strong> (instance: CodeMirror)</code></dt>
484 <dd>Will be fired whenever CodeMirror updates its DOM display.</dd>
486 <dt id="event_renderLine"><code><strong>"renderLine"</strong> (instance: CodeMirror, line: LineHandle, element: Element)</code></dt>
487 <dd>Fired whenever a line is (re-)rendered to the DOM. Fired
488 right after the DOM element is built, <em>before</em> it is
489 added to the document. The handler may mess with the style of
490 the resulting element, or add event handlers, but
491 should <em>not</em> try to change the state of the editor.</dd>
493 <dt id="event_dom"><code><strong>"mousedown"</strong>,
494 <strong>"dblclick"</strong>, <strong>"contextmenu"</strong>, <strong>"keydown"</strong>, <strong>"keypress"</strong>,
495 <strong>"keyup"</strong>, <strong>"dragstart"</strong>, <strong>"dragenter"</strong>,
496 <strong>"dragover"</strong>, <strong>"drop"</strong>
497 (instance: CodeMirror, event: Event)</code></dt>
498 <dd>Fired when CodeMirror is handling a DOM event of this type.
499 You can <code>preventDefault</code> the event, or give it a
500 truthy <code>codemirrorIgnore</code> property, to signal that
501 CodeMirror should do no further handling.</dd>
504 <p>Document objects (instances
505 of <a href="#Doc"><code>CodeMirror.Doc</code></a>) emit the
506 following events:</p>
509 <dt id="event_doc_change"><code><strong>"change"</strong> (doc: CodeMirror.Doc, changeObj: object)</code></dt>
510 <dd>Fired whenever a change occurs to the
511 document. <code>changeObj</code> has a similar type as the
513 editor's <a href="#event_change"><code>"change"</code></a>
514 event, but it never has a <code>next</code> property, because
515 document change events are not batched (whereas editor change
518 <dt id="event_doc_beforeChange"><code><strong>"beforeChange"</strong> (doc: CodeMirror.Doc, change: object)</code></dt>
519 <dd>See the <a href="#event_beforeChange">description of the
520 same event</a> on editor instances.</dd>
522 <dt id="event_doc_cursorActivity"><code><strong>"cursorActivity"</strong> (doc: CodeMirror.Doc)</code></dt>
523 <dd>Fired whenever the cursor or selection in this document
526 <dt id="event_doc_beforeSelectionChange"><code><strong>"beforeSelectionChange"</strong> (doc: CodeMirror.Doc, selection: {head, anchor})</code></dt>
528 the <a href="#event_beforeSelectionChange">event by the same
529 name</a> as fired on editor instances.</dd>
532 <p>Line handles (as returned by, for
533 example, <a href="#getLineHandle"><code>getLineHandle</code></a>)
534 support these events:</p>
537 <dt id="event_delete"><code><strong>"delete"</strong> ()</code></dt>
538 <dd>Will be fired when the line object is deleted. A line object
539 is associated with the <em>start</em> of the line. Mostly useful
540 when you need to find out when your <a href="#setGutterMarker">gutter
541 markers</a> on a given line are removed.</dd>
542 <dt id="event_line_change"><code><strong>"change"</strong> (line: LineHandle, changeObj: object)</code></dt>
543 <dd>Fires when the line's text content is changed in any way
544 (but the line is not deleted outright). The <code>change</code>
545 object is similar to the one passed
546 to <a href="#event_change">change event</a> on the editor
550 <p>Marked range handles (<code>CodeMirror.TextMarker</code>), as returned
551 by <a href="#markText"><code>markText</code></a>
552 and <a href="#setBookmark"><code>setBookmark</code></a>, emit the
553 following events:</p>
556 <dt id="event_beforeCursorEnter"><code><strong>"beforeCursorEnter"</strong> ()</code></dt>
557 <dd>Fired when the cursor enters the marked range. From this
558 event handler, the editor state may be inspected
559 but <em>not</em> modified, with the exception that the range on
560 which the event fires may be cleared.</dd>
561 <dt id="event_clear"><code><strong>"clear"</strong> (from: {line, ch}, to: {line, ch})</code></dt>
562 <dd>Fired when the range is cleared, either through cursor
563 movement in combination
564 with <a href="#mark_clearOnEnter"><code>clearOnEnter</code></a>
565 or through a call to its <code>clear()</code> method. Will only
566 be fired once per handle. Note that deleting the range through
567 text editing does not fire this event, because an undo action
568 might bring the range back into existence. <code>from</code>
569 and <code>to</code> give the part of the document that the range
570 spanned when it was cleared.</dd>
571 <dt id="event_hide"><code><strong>"hide"</strong> ()</code></dt>
572 <dd>Fired when the last part of the marker is removed from the
573 document by editing operations.</dd>
574 <dt id="event_unhide"><code><strong>"unhide"</strong> ()</code></dt>
575 <dd>Fired when, after the marker was removed by editing, a undo
576 operation brought the marker back.</dd>
579 <p>Line widgets (<code>CodeMirror.LineWidget</code>), returned
580 by <a href="#addLineWidget"><code>addLineWidget</code></a>, fire
584 <dt id="event_redraw"><code><strong>"redraw"</strong> ()</code></dt>
585 <dd>Fired whenever the editor re-adds the widget to the DOM.
586 This will happen once right after the widget is added (if it is
587 scrolled into view), and then again whenever it is scrolled out
588 of view and back in again, or when changes to the editor options
589 or the line the widget is on require the widget to be
593 <h2 id="keymaps">Keymaps</h2>
595 <p>Keymaps are ways to associate keys with functionality. A keymap
596 is an object mapping strings that identify the keys to functions
597 that implement their functionality.</p>
599 <p>Keys are identified either by name or by character.
600 The <code>CodeMirror.keyNames</code> object defines names for
601 common keys and associates them with their key codes. Examples of
602 names defined here are <code>Enter</code>, <code>F5</code>,
603 and <code>Q</code>. These can be prefixed
604 with <code>Shift-</code>, <code>Cmd-</code>, <code>Ctrl-</code>,
605 and <code>Alt-</code> (in that order!) to specify a modifier. So
606 for example, <code>Shift-Ctrl-Space</code> would be a valid key
609 <p>Common example: map the Tab key to insert spaces instead of a tab
612 <pre data-lang="javascript">
615 var spaces = Array(cm.getOption("indentUnit") + 1).join(" ");
616 cm.replaceSelection(spaces, "end", "+input");
620 <p>Alternatively, a character can be specified directly by
621 surrounding it in single quotes, for example <code>'$'</code>
622 or <code>'q'</code>. Due to limitations in the way browsers fire
623 key events, these may not be prefixed with modifiers.</p>
625 <p>The <code>CodeMirror.keyMap</code> object associates keymaps
626 with names. User code and keymap definitions can assign extra
627 properties to this object. Anywhere where a keymap is expected, a
628 string can be given, which will be looked up in this object. It
629 also contains the <code>"default"</code> keymap holding the
630 default bindings.</p>
632 <p id="commands">The values of properties in keymaps can be either functions of
633 a single argument (the CodeMirror instance), strings, or
634 <code>false</code>. Such strings refer to properties of the
635 <code>CodeMirror.commands</code> object, which defines a number of
636 common commands that are used by the default keybindings, and maps
637 them to functions. If the property is set to <code>false</code>,
638 CodeMirror leaves handling of the key up to the browser. A key
639 handler function may return <code>CodeMirror.Pass</code> to indicate
640 that it has decided not to handle the key, and other handlers (or
641 the default behavior) should be given a turn.</p>
643 <p>Keys mapped to command names that start with the
644 characters <code>"go"</code> (which should be used for
645 cursor-movement actions) will be fired even when an
646 extra <code>Shift</code> modifier is present (i.e. <code>"Up":
647 "goLineUp"</code> matches both up and shift-up). This is used to
648 easily implement shift-selection.</p>
650 <p>Keymaps can defer to each other by defining
651 a <code>fallthrough</code> property. This indicates that when a
652 key is not found in the map itself, one or more other maps should
653 be searched. It can hold either a single keymap or an array of
656 <p>When a keymap contains a <code>nofallthrough</code> property
657 set to <code>true</code>, keys matched against that map will be
658 ignored if they don't match any of the bindings in the map (no
659 further child maps will be tried). When
660 the <code>disableInput</code> property is set
661 to <code>true</code>, the default effect of inserting a character
662 will be suppressed when the keymap is active as the top-level
665 <h2 id="styling">Customized Styling</h2>
667 <p>Up to a certain extent, CodeMirror's look can be changed by
668 modifying style sheet files. The style sheets supplied by modes
669 simply provide the colors for that mode, and can be adapted in a
670 very straightforward way. To style the editor itself, it is
671 possible to alter or override the styles defined
672 in <a href="../lib/codemirror.css"><code>codemirror.css</code></a>.</p>
674 <p>Some care must be taken there, since a lot of the rules in this
675 file are necessary to have CodeMirror function properly. Adjusting
676 colors should be safe, of course, and with some care a lot of
677 other things can be changed as well. The CSS classes defined in
678 this file serve the following roles:</p>
681 <dt id="class_CodeMirror"><code><strong>CodeMirror</strong></code></dt>
682 <dd>The outer element of the editor. This should be used for the
683 editor width, height, borders and positioning. Can also be used
684 to set styles that should hold for everything inside the editor
685 (such as font and font size), or to set a background.</dd>
687 <dt id="class_CodeMirror_scroll"><code><strong>CodeMirror-scroll</strong></code></dt>
688 <dd>Whether the editor scrolls (<code>overflow: auto</code> +
689 fixed height). By default, it does. Setting
690 the <code>CodeMirror</code> class to have <code>height:
691 auto</code> and giving this class <code>overflow-x: auto;
692 overflow-y: hidden;</code> will cause the editor
693 to <a href="../demo/resize.html">resize to fit its
696 <dt id="class_CodeMirror_focused"><code><strong>CodeMirror-focused</strong></code></dt>
697 <dd>Whenever the editor is focused, the top element gets this
698 class. This is used to hide the cursor and give the selection a
699 different color when the editor is not focused.</dd>
701 <dt id="class_CodeMirror_gutters"><code><strong>CodeMirror-gutters</strong></code></dt>
702 <dd>This is the backdrop for all gutters. Use it to set the
703 default gutter background color, and optionally add a border on
704 the right of the gutters.</dd>
706 <dt id="class_CodeMirror_linenumbers"><code><strong>CodeMirror-linenumbers</strong></code></dt>
707 <dd>Use this for giving a background or width to the line number
710 <dt id="class_CodeMirror_linenumber"><code><strong>CodeMirror-linenumber</strong></code></dt>
711 <dd>Used to style the actual individual line numbers. These
712 won't be children of the <code>CodeMirror-linenumbers</code>
713 (plural) element, but rather will be absolutely positioned to
714 overlay it. Use this to set alignment and text properties for
715 the line numbers.</dd>
717 <dt id="class_CodeMirror_lines"><code><strong>CodeMirror-lines</strong></code></dt>
718 <dd>The visible lines. This is where you specify vertical
719 padding for the editor content.</dd>
721 <dt id="class_CodeMirror_cursor"><code><strong>CodeMirror-cursor</strong></code></dt>
722 <dd>The cursor is a block element that is absolutely positioned.
723 You can make it look whichever way you want.</dd>
725 <dt id="class_CodeMirror_selected"><code><strong>CodeMirror-selected</strong></code></dt>
726 <dd>The selection is represented by <code>span</code> elements
727 with this class.</dd>
729 <dt id="class_CodeMirror_matchingbracket"><code><strong>CodeMirror-matchingbracket</strong></code>,
730 <code><strong>CodeMirror-nonmatchingbracket</strong></code></dt>
731 <dd>These are used to style matched (or unmatched) brackets.</dd>
734 <p>If your page's style sheets do funky things to
735 all <code>div</code> or <code>pre</code> elements (you probably
736 shouldn't do that), you'll have to define rules to cancel these
737 effects out again for elements under the <code>CodeMirror</code>
740 <p>Themes are also simply CSS files, which define colors for
741 various syntactic elements. See the files in
742 the <a href="../theme/"><code>theme</code></a> directory.</p>
744 <h2 id="api">Programming API</h2>
746 <p>A lot of CodeMirror features are only available through its
747 API. Thus, you need to write code (or
748 use <a href="#addons">addons</a>) if you want to expose them to
751 <p>Whenever points in the document are represented, the API uses
752 objects with <code>line</code> and <code>ch</code> properties.
753 Both are zero-based. CodeMirror makes sure to 'clip' any positions
754 passed by client code so that they fit inside the document, so you
755 shouldn't worry too much about sanitizing your coordinates. If you
756 give <code>ch</code> a value of <code>null</code>, or don't
757 specify it, it will be replaced with the length of the specified
760 <p>Methods prefixed with <code>doc.</code> can, unless otherwise
761 specified, be called both on <code>CodeMirror</code> (editor)
762 instances and <code>CodeMirror.Doc</code> instances. Methods
763 prefixed with <code>cm.</code> are <em>only</em> available
764 on <code>CodeMirror</code> instances.</p>
766 <h3 id="api_constructor">Constructor</h3>
768 <p id="CodeMirror">Constructing an editor instance is done with
769 the <code><strong>CodeMirror</strong>(place: Element|fn(Element),
770 ?option: object)</code> constructor. If the <code>place</code>
771 argument is a DOM element, the editor will be appended to it. If
772 it is a function, it will be called, and is expected to place the
773 editor into the document. <code>options</code> may be an element
774 mapping <a href="#config">option names</a> to values. The options
775 that it doesn't explicitly specify (or all options, if it is not
776 passed) will be taken
777 from <a href="#defaults"><code>CodeMirror.defaults</code></a>.</p>
779 <p>Note that the options object passed to the constructor will be
780 mutated when the instance's options
781 are <a href="#setOption">changed</a>, so you shouldn't share such
782 objects between instances.</p>
784 <p>See <a href="#fromTextArea"><code>CodeMirror.fromTextArea</code></a>
785 for another way to construct an editor instance.</p>
787 <h3 id="api_content">Content manipulation methods</h3>
790 <dt id="getValue"><code><strong>doc.getValue</strong>(?separator: string) → string</code></dt>
791 <dd>Get the current editor content. You can pass it an optional
792 argument to specify the string to be used to separate lines
793 (defaults to <code>"\n"</code>).</dd>
794 <dt id="setValue"><code><strong>doc.setValue</strong>(content: string)</code></dt>
795 <dd>Set the editor content.</dd>
797 <dt id="getRange"><code><strong>doc.getRange</strong>(from: {line, ch}, to: {line, ch}, ?separator: string) → string</code></dt>
798 <dd>Get the text between the given points in the editor, which
799 should be <code>{line, ch}</code> objects. An optional third
800 argument can be given to indicate the line separator string to
801 use (defaults to <code>"\n"</code>).</dd>
802 <dt id="replaceRange"><code><strong>doc.replaceRange</strong>(replacement: string, from: {line, ch}, to: {line, ch})</code></dt>
803 <dd>Replace the part of the document between <code>from</code>
804 and <code>to</code> with the given string. <code>from</code>
805 and <code>to</code> must be <code>{line, ch}</code>
806 objects. <code>to</code> can be left off to simply insert the
807 string at position <code>from</code>.</dd>
809 <dt id="getLine"><code><strong>doc.getLine</strong>(n: integer) → string</code></dt>
810 <dd>Get the content of line <code>n</code>.</dd>
811 <dt id="setLine"><code><strong>doc.setLine</strong>(n: integer, text: string)</code></dt>
812 <dd>Set the content of line <code>n</code>.</dd>
813 <dt id="removeLine"><code><strong>doc.removeLine</strong>(n: integer)</code></dt>
814 <dd>Remove the given line from the document.</dd>
816 <dt id="lineCount"><code><strong>doc.lineCount</strong>() → integer</code></dt>
817 <dd>Get the number of lines in the editor.</dd>
818 <dt id="firstLine"><code><strong>doc.firstLine</strong>() → integer</code></dt>
819 <dd>Get the first line of the editor. This will
820 usually be zero but for <a href="#linkedDoc_from">linked sub-views</a>,
821 or <a href="#api_doc">documents</a> instantiated with a non-zero
822 first line, it might return other values.</dd>
823 <dt id="lastLine"><code><strong>doc.lastLine</strong>() → integer</code></dt>
824 <dd>Get the last line of the editor. This will
825 usually be <code>doc.lineCount() - 1</code>,
826 but for <a href="#linkedDoc_from">linked sub-views</a>,
827 it might return other values.</dd>
829 <dt id="getLineHandle"><code><strong>doc.getLineHandle</strong>(num: integer) → LineHandle</code></dt>
830 <dd>Fetches the line handle for the given line number.</dd>
831 <dt id="getLineNumber"><code><strong>doc.getLineNumber</strong>(handle: LineHandle) → integer</code></dt>
832 <dd>Given a line handle, returns the current position of that
833 line (or <code>null</code> when it is no longer in the
835 <dt id="eachLine"><code><strong>doc.eachLine</strong>(f: (line: LineHandle))</code></dt>
836 <dt><code><strong>doc.eachLine</strong>(start: integer, end: integer, f: (line: LineHandle))</code></dt>
837 <dd>Iterate over the whole document, or if <code>start</code>
838 and <code>end</code> line numbers are given, the range
839 from <code>start</code> up to (not including) <code>end</code>,
840 and call <code>f</code> for each line, passing the line handle.
841 This is a faster way to visit a range of line handlers than
842 calling <a href="#getLineHandle"><code>getLineHandle</code></a>
843 for each of them. Note that line handles have
844 a <code>text</code> property containing the line's content (as a
847 <dt id="markClean"><code><strong>doc.markClean</strong>()</code></dt>
848 <dd>Set the editor content as 'clean', a flag that it will
849 retain until it is edited, and which will be set again when such
850 an edit is undone again. Useful to track whether the content
851 needs to be saved. This function is deprecated in favor
852 of <a href="#changeGeneration"><code>changeGeneration</code></a>,
853 which allows multiple subsystems to track different notions of
854 cleanness without interfering.</dd>
855 <dt id="changeGeneration"><code><strong>doc.changeGeneration</strong>() → integer</code></dt>
856 <dd>Returns a number that can later be passed
857 to <a href="#isClean"><code>isClean</code></a> to test whether
858 any edits were made (and not undone) in the meantime.</dd>
859 <dt id="isClean"><code><strong>doc.isClean</strong>(?generation: integer) → boolean</code></dt>
860 <dd>Returns whether the document is currently clean — not
861 modified since initialization or the last call
862 to <a href="#markClean"><code>markClean</code></a> if no
863 argument is passed, or since the matching call
864 to <a href="#changeGeneration"><code>changeGeneration</code></a>
865 if a generation value is given.</dd>
868 <h3 id="api_selection">Cursor and selection methods</h3>
871 <dt id="getSelection"><code><strong>doc.getSelection</strong>() → string</code></dt>
872 <dd>Get the currently selected code.</dd>
873 <dt id="replaceSelection"><code><strong>doc.replaceSelection</strong>(replacement: string, ?collapse: string)</code></dt>
874 <dd>Replace the selection with the given string. By default, the
875 new selection will span the inserted text. The
876 optional <code>collapse</code> argument can be used to change
877 this—passing <code>"start"</code> or <code>"end"</code> will
878 collapse the selection to the start or end of the inserted
881 <dt id="getCursor"><code><strong>doc.getCursor</strong>(?start: string) → {line, ch}</code></dt>
882 <dd><code>start</code> is a an optional string indicating which
883 end of the selection to return. It may
884 be <code>"start"</code>, <code>"end"</code>, <code>"head"</code>
885 (the side of the selection that moves when you press
886 shift+arrow), or <code>"anchor"</code> (the fixed side of the
887 selection). Omitting the argument is the same as
888 passing <code>"head"</code>. A <code>{line, ch}</code> object
889 will be returned.</dd>
890 <dt id="somethingSelected"><code><strong>doc.somethingSelected</strong>() → boolean</code></dt>
891 <dd>Return true if any text is selected.</dd>
892 <dt id="setCursor"><code><strong>doc.setCursor</strong>(pos: {line, ch})</code></dt>
893 <dd>Set the cursor position. You can either pass a
894 single <code>{line, ch}</code> object, or the line and the
895 character as two separate parameters.</dd>
896 <dt id="setSelection"><code><strong>doc.setSelection</strong>(anchor: {line, ch}, head: {line, ch})</code></dt>
897 <dd>Set the selection range. <code>anchor</code>
898 and <code>head</code> should be <code>{line, ch}</code>
899 objects. <code>head</code> defaults to <code>anchor</code> when
901 <dt id="extendSelection"><code><strong>doc.extendSelection</strong>(from: {line, ch}, ?to: {line, ch})</code></dt>
903 to <a href="#setSelection"><code>setSelection</code></a>, but
904 will, if shift is held or
905 the <a href="#setExtending">extending</a> flag is set, move the
906 head of the selection while leaving the anchor at its current
907 place. <code>pos2</code> is optional, and can be passed to
908 ensure a region (for example a word or paragraph) will end up
909 selected (in addition to whatever lies between that region and
910 the current anchor).</dd>
911 <dt id="setExtending"><code><strong>doc.setExtending</strong>(value: boolean)</code></dt>
912 <dd>Sets or clears the 'extending' flag, which acts similar to
913 the shift key, in that it will cause cursor movement and calls
914 to <a href="#extendSelection"><code>extendSelection</code></a>
915 to leave the selection anchor in place.</dd>
917 <dt id="hasFocus"><code><strong>cm.hasFocus</strong>() → boolean</code></dt>
918 <dd>Tells you whether the editor currently has focus.</dd>
920 <dt id="findPosH"><code><strong>cm.findPosH</strong>(start: {line, ch}, amount: integer, unit: string, visually: boolean) → {line, ch, ?hitSide: boolean}</code></dt>
921 <dd>Used to find the target position for horizontal cursor
922 motion. <code>start</code> is a <code>{line, ch}</code>
923 object, <code>amount</code> an integer (may be negative),
924 and <code>unit</code> one of the
925 string <code>"char"</code>, <code>"column"</code>,
926 or <code>"word"</code>. Will return a position that is produced
927 by moving <code>amount</code> times the distance specified
928 by <code>unit</code>. When <code>visually</code> is true, motion
929 in right-to-left text will be visual rather than logical. When
930 the motion was clipped by hitting the end or start of the
931 document, the returned value will have a <code>hitSide</code>
932 property set to true.</dd>
933 <dt id="findPosV"><code><strong>cm.findPosV</strong>(start: {line, ch}, amount: integer, unit: string) → {line, ch, ?hitSide: boolean}</code></dt>
934 <dd>Similar to <a href="#findPosH"><code>findPosH</code></a>,
935 but used for vertical motion. <code>unit</code> may
936 be <code>"line"</code> or <code>"page"</code>. The other
937 arguments and the returned value have the same interpretation as
938 they have in <code>findPosH</code>.</dd>
941 <h3 id="api_configuration">Configuration methods</h3>
944 <dt id="setOption"><code><strong>cm.setOption</strong>(option: string, value: any)</code></dt>
945 <dd>Change the configuration of the editor. <code>option</code>
946 should the name of an <a href="#config">option</a>,
947 and <code>value</code> should be a valid value for that
949 <dt id="getOption"><code><strong>cm.getOption</strong>(option: string) → any</code></dt>
950 <dd>Retrieves the current value of the given option for this
951 editor instance.</dd>
953 <dt id="addKeyMap"><code><strong>cm.addKeyMap</strong>(map: object, bottom: boolean)</code></dt>
954 <dd>Attach an additional <a href="#keymaps">keymap</a> to the
955 editor. This is mostly useful for addons that need to register
956 some key handlers without trampling on
957 the <a href="#option_extraKeys"><code>extraKeys</code></a>
958 option. Maps added in this way have a higher precedence than
959 the <code>extraKeys</code>
960 and <a href="#option_keyMap"><code>keyMap</code></a> options,
961 and between them, the maps added earlier have a lower precedence
962 than those added later, unless the <code>bottom</code> argument
963 was passed, in which case they end up below other keymaps added
964 with this method.</dd>
965 <dt id="removeKeyMap"><code><strong>cm.removeKeyMap</strong>(map: object)</code></dt>
966 <dd>Disable a keymap added
967 with <a href="#addKeyMap"><code>addKeyMap</code></a>. Either
968 pass in the keymap object itself, or a string, which will be
969 compared against the <code>name</code> property of the active
972 <dt id="addOverlay"><code><strong>cm.addOverlay</strong>(mode: string|object, ?options: object)</code></dt>
973 <dd>Enable a highlighting overlay. This is a stateless mini-mode
974 that can be used to add extra highlighting. For example,
975 the <a href="../demo/search.html">search addon</a> uses it to
976 highlight the term that's currently being
977 searched. <code>mode</code> can be a <a href="#option_mode">mode
978 spec</a> or a mode object (an object with
979 a <a href="#token"><code>token</code></a> method).
980 The <code>options</code> parameter is optional. If given, it
981 should be an object. Currently, only the <code>opaque</code>
982 option is recognized. This defaults to off, but can be given to
983 allow the overlay styling, when not <code>null</code>, to
984 override the styling of the base mode entirely, instead of the
985 two being applied together.</dd>
986 <dt id="removeOverlay"><code><strong>cm.removeOverlay</strong>(mode: string|object)</code></dt>
987 <dd>Pass this the exact value passed for the <code>mode</code>
988 parameter to <a href="#addOverlay"><code>addOverlay</code></a>,
989 or a string that corresponds to the <code>name</code> propery of
990 that value, to remove an overlay again.</dd>
992 <dt id="on"><code><strong>cm.on</strong>(type: string, func: (...args))</code></dt>
993 <dd>Register an event handler for the given event type (a
994 string) on the editor instance. There is also
995 a <code>CodeMirror.on(object, type, func)</code> version
996 that allows registering of events on any object.</dd>
997 <dt id="off"><code><strong>cm.off</strong>(type: string, func: (...args))</code></dt>
998 <dd>Remove an event handler on the editor instance. An
999 equivalent <code>CodeMirror.off(object, type,
1000 func)</code> also exists.</dd>
1003 <h3 id="api_doc">Document management methods</h3>
1005 <p id="Doc">Each editor is associated with an instance
1006 of <code>CodeMirror.Doc</code>, its document. A document
1007 represents the editor content, plus a selection, an undo history,
1008 and a <a href="#option_mode">mode</a>. A document can only be
1009 associated with a single editor at a time. You can create new
1010 documents by calling the <code>CodeMirror.Doc(text, mode,
1011 firstLineNumber)</code> constructor. The last two arguments are
1012 optional and can be used to set a mode for the document and make
1013 it start at a line number other than 0, respectively.</p>
1016 <dt id="getDoc"><code><strong>cm.getDoc</strong>() → Doc</code></dt>
1017 <dd>Retrieve the currently active document from an editor.</dd>
1018 <dt id="getEditor"><code><strong>doc.getEditor</strong>() → CodeMirror</code></dt>
1019 <dd>Retrieve the editor associated with a document. May
1020 return <code>null</code>.</dd>
1022 <dt id="swapDoc"><code><strong>cm.swapDoc</strong>(doc: CodeMirror.Doc) → Doc</code></dt>
1023 <dd>Attach a new document to the editor. Returns the old
1024 document, which is now no longer associated with an editor.</dd>
1026 <dt id="copy"><code><strong>doc.copy</strong>(copyHistory: boolean) → Doc</code></dt>
1027 <dd>Create an identical copy of the given doc.
1028 When <code>copyHistory</code> is true, the history will also be
1029 copied. Can not be called directly on an editor.</dd>
1031 <dt id="linkedDoc"><code><strong>doc.linkedDoc</strong>(options: object) → Doc</code></dt>
1032 <dd>Create a new document that's linked to the target document.
1033 Linked documents will stay in sync (changes to one are also
1034 applied to the other) until <a href="#unlinkDoc">unlinked</a>.
1035 These are the options that are supported:
1037 <dt id="linkedDoc_sharedHist"><code><strong>sharedHist</strong>: boolean</code></dt>
1038 <dd>When turned on, the linked copy will share an undo
1039 history with the original. Thus, something done in one of
1040 the two can be undone in the other, and vice versa.</dd>
1041 <dt id="linkedDoc_from"><code><strong>from</strong>: integer</code></dt>
1042 <dt id="linkedDoc_to"><code><strong>to</strong>: integer</code></dt>
1043 <dd>Can be given to make the new document a subview of the
1044 original. Subviews only show a given range of lines. Note
1045 that line coordinates inside the subview will be consistent
1046 with those of the parent, so that for example a subview
1047 starting at line 10 will refer to its first line as line 10,
1049 <dt id="linkedDoc_mode"><code><strong>mode</strong>: string|object</code></dt>
1050 <dd>By default, the new document inherits the mode of the
1051 parent. This option can be set to
1052 a <a href="#option_mode">mode spec</a> to give it a
1053 different mode.</dd>
1055 <dt id="unlinkDoc"><code><strong>doc.unlinkDoc</strong>(doc: CodeMirror.Doc)</code></dt>
1056 <dd>Break the link between two documents. After calling this,
1057 changes will no longer propagate between the documents, and, if
1058 they had a shared history, the history will become
1060 <dt id="iterLinkedDocs"><code><strong>doc.iterLinkedDocs</strong>(function: (doc: CodeMirror.Doc, sharedHist: boolean))</code></dt>
1061 <dd>Will call the given function for all documents linked to the
1062 target document. It will be passed two arguments, the linked document
1063 and a boolean indicating whether that document shares history
1064 with the target.</dd>
1067 <h3 id="api_history">History-related methods</h3>
1070 <dt id="undo"><code><strong>doc.undo</strong>()</code></dt>
1071 <dd>Undo one edit (if any undo events are stored).</dd>
1072 <dt id="redo"><code><strong>doc.redo</strong>()</code></dt>
1073 <dd>Redo one undone edit.</dd>
1075 <dt id="historySize"><code><strong>doc.historySize</strong>() → {undo: integer, redo: integer}</code></dt>
1076 <dd>Returns an object with <code>{undo, redo}</code> properties,
1077 both of which hold integers, indicating the amount of stored
1078 undo and redo operations.</dd>
1079 <dt id="clearHistory"><code><strong>doc.clearHistory</strong>()</code></dt>
1080 <dd>Clears the editor's undo history.</dd>
1081 <dt id="getHistory"><code><strong>doc.getHistory</strong>() → object</code></dt>
1082 <dd>Get a (JSON-serializeable) representation of the undo history.</dd>
1083 <dt id="setHistory"><code><strong>doc.setHistory</strong>(history: object)</code></dt>
1084 <dd>Replace the editor's undo history with the one provided,
1085 which must be a value as returned
1086 by <a href="#getHistory"><code>getHistory</code></a>. Note that
1087 this will have entirely undefined results if the editor content
1088 isn't also the same as it was when <code>getHistory</code> was
1092 <h3 id="api_marker">Text-marking methods</h3>
1095 <dt id="markText"><code><strong>doc.markText</strong>(from: {line, ch}, to: {line, ch}, ?options: object) → TextMarker</code></dt>
1096 <dd>Can be used to mark a range of text with a specific CSS
1097 class name. <code>from</code> and <code>to</code> should
1098 be <code>{line, ch}</code> objects. The <code>options</code>
1099 parameter is optional. When given, it should be an object that
1100 may contain the following configuration options:
1102 <dt id="mark_className"><code><strong>className</strong>: string</code></dt>
1103 <dd>Assigns a CSS class to the marked stretch of text.</dd>
1104 <dt id="mark_inclusiveLeft"><code><strong>inclusiveLeft</strong>: boolean</code></dt>
1105 <dd>Determines whether
1106 text inserted on the left of the marker will end up inside
1107 or outside of it.</dd>
1108 <dt id="mark_inclusiveRight"><code><strong>inclusiveRight</strong>: boolean</code></dt>
1109 <dd>Like <code>inclusiveLeft</code>,
1110 but for the right side.</dd>
1111 <dt id="mark_atomic"><code><strong>atomic</strong>: boolean</code></dt>
1112 <dd>Atomic ranges act as a single unit when cursor movement is
1113 concerned—i.e. it is impossible to place the cursor inside of
1114 them. In atomic ranges, <code>inclusiveLeft</code>
1115 and <code>inclusiveRight</code> have a different meaning—they
1116 will prevent the cursor from being placed respectively
1117 directly before and directly after the range.</dd>
1118 <dt id="mark_collapsed"><code><strong>collapsed</strong>: boolean</code></dt>
1119 <dd>Collapsed ranges do not show up in the display. Setting a
1120 range to be collapsed will automatically make it atomic.</dd>
1121 <dt id="mark_clearOnEnter"><code><strong>clearOnEnter</strong>: boolean</code></dt>
1122 <dd>When enabled, will cause the mark to clear itself whenever
1123 the cursor enters its range. This is mostly useful for
1124 text-replacement widgets that need to 'snap open' when the
1125 user tries to edit them. The
1126 <a href="#event_clear"><code>"clear"</code></a> event
1127 fired on the range handle can be used to be notified when this
1129 <dt id="mark_replacedWith"><code><strong>replacedWith</strong>: Element</code></dt>
1130 <dd>Use a given node to display this range. Implies both
1131 collapsed and atomic. The given DOM node <em>must</em> be an
1132 inline element (as opposed to a block element).</dd>
1133 <dt><code><strong>handleMouseEvents</strong>: boolean</code></dt>
1134 <dd>When <code>replacedWith</code> is given, this determines
1135 whether the editor will capture mouse and drag events
1136 occurring in this widget. Default is false—the events will be
1137 left alone for the default browser handler, or specific
1138 handlers on the widget, to capture.</dd>
1139 <dt id="mark_readOnly"><code><strong>readOnly</strong>: boolean</code></dt>
1140 <dd>A read-only span can, as long as it is not cleared, not be
1142 calling <a href="#setValue"><code>setValue</code></a> to reset
1143 the whole document. <em>Note:</em> adding a read-only span
1144 currently clears the undo history of the editor, because
1145 existing undo events being partially nullified by read-only
1146 spans would corrupt the history (in the current
1147 implementation).</dd>
1148 <dt id="mark_addToHistory"><code><strong>addToHistory</strong>: boolean</code></dt>
1149 <dd>When set to true (default is false), adding this marker
1150 will create an event in the undo history that can be
1151 individually undone (clearing the marker).</dd>
1152 <dt id="mark_startStyle"><code><strong>startStyle</strong>: string</code></dt><dd>Can be used to specify
1153 an extra CSS class to be applied to the leftmost span that
1154 is part of the marker.</dd>
1155 <dt id="mark_endStyle"><code><strong>endStyle</strong>: string</code></dt><dd>Equivalent
1156 to <code>startStyle</code>, but for the rightmost span.</dd>
1157 <dt id="mark_title"><code><strong>title</strong>:
1158 string</code></dt><dd>When given, will give the nodes created
1159 for this span a HTML <code>title</code> attribute with the
1161 <dt id="mark_shared"><code><strong>shared</strong>: boolean</code></dt><dd>When the
1162 target document is <a href="#linkedDoc">linked</a> to other
1163 documents, you can set <code>shared</code> to true to make the
1164 marker appear in all documents. By default, a marker appears
1165 only in its target document.</dd>
1167 The method will return an object that represents the marker
1168 (with constuctor <code>CodeMirror.TextMarker</code>), which
1169 exposes three methods:
1170 <code><strong>clear</strong>()</code>, to remove the mark,
1171 <code><strong>find</strong>()</code>, which returns
1172 a <code>{from, to}</code> object (both holding document
1173 positions), indicating the current position of the marked range,
1174 or <code>undefined</code> if the marker is no longer in the
1175 document, and finally <code><strong>changed</strong>()</code>,
1176 which you can call if you've done something that might change
1177 the size of the marker (for example changing the content of
1178 a <a href="#mark_replacedWith"><code>replacedWith</code></a>
1179 node), and want to cheaply update the display.</dd>
1181 <dt id="setBookmark"><code><strong>doc.setBookmark</strong>(pos: {line, ch}, ?options: object) → TextMarker</code></dt>
1182 <dd>Inserts a bookmark, a handle that follows the text around it
1183 as it is being edited, at the given position. A bookmark has two
1184 methods <code>find()</code> and <code>clear()</code>. The first
1185 returns the current position of the bookmark, if it is still in
1186 the document, and the second explicitly removes the bookmark.
1187 The options argument is optional. If given, the following
1188 properties are recognized:
1190 <dt><code><strong>widget</strong>: Element</code></dt><dd>Can be used to display a DOM
1191 node at the current location of the bookmark (analogous to
1192 the <a href="#mark_replacedWith"><code>replacedWith</code></a>
1193 option to <code>markText</code>).</dd>
1194 <dt><code><strong>insertLeft</strong>: boolean</code></dt><dd>By default, text typed
1195 when the cursor is on top of the bookmark will end up to the
1196 right of the bookmark. Set this option to true to make it go
1197 to the left instead.</dd>
1200 <dt id="findMarksAt"><code><strong>doc.findMarksAt</strong>(pos: {line, ch}) → array<TextMarker></code></dt>
1201 <dd>Returns an array of all the bookmarks and marked ranges
1202 present at the given position.</dd>
1203 <dt id="getAllMarks"><code><strong>doc.getAllMarks</strong>() → array<TextMarker></code></dt>
1204 <dd>Returns an array containing all marked ranges in the document.</dd>
1207 <h3 id="api_decoration">Widget, gutter, and decoration methods</h3>
1210 <dt id="setGutterMarker"><code><strong>cm.setGutterMarker</strong>(line: integer|LineHandle, gutterID: string, value: Element) → LineHandle</code></dt>
1211 <dd>Sets the gutter marker for the given gutter (identified by
1213 the <a href="#option_gutters"><code>gutters</code></a> option)
1214 to the given value. Value can be either <code>null</code>, to
1215 clear the marker, or a DOM element, to set it. The DOM element
1216 will be shown in the specified gutter next to the specified
1219 <dt id="clearGutter"><code><strong>cm.clearGutter</strong>(gutterID: string)</code></dt>
1220 <dd>Remove all gutter markers in
1221 the <a href="#option_gutters">gutter</a> with the given ID.</dd>
1223 <dt id="addLineClass"><code><strong>cm.addLineClass</strong>(line: integer|LineHandle, where: string, class: string) → LineHandle</code></dt>
1224 <dd>Set a CSS class name for the given line. <code>line</code>
1225 can be a number or a line handle. <code>where</code> determines
1226 to which element this class should be applied, can can be one
1227 of <code>"text"</code> (the text element, which lies in front of
1228 the selection), <code>"background"</code> (a background element
1229 that will be behind the selection), or <code>"wrap"</code> (the
1230 wrapper node that wraps all of the line's elements, including
1231 gutter elements). <code>class</code> should be the name of the
1232 class to apply.</dd>
1234 <dt id="removeLineClass"><code><strong>cm.removeLineClass</strong>(line: integer|LineHandle, where: string, class: string) → LineHandle</code></dt>
1235 <dd>Remove a CSS class from a line. <code>line</code> can be a
1236 line handle or number. <code>where</code> should be one
1237 of <code>"text"</code>, <code>"background"</code>,
1238 or <code>"wrap"</code>
1239 (see <a href="#addLineClass"><code>addLineClass</code></a>). <code>class</code>
1240 can be left off to remove all classes for the specified node, or
1241 be a string to remove only a specific class.</dd>
1243 <dt id="lineInfo"><code><strong>cm.lineInfo</strong>(line: integer|LineHandle) → object</code></dt>
1244 <dd>Returns the line number, text content, and marker status of
1245 the given line, which can be either a number or a line handle.
1246 The returned object has the structure <code>{line, handle, text,
1247 gutterMarkers, textClass, bgClass, wrapClass, widgets}</code>,
1248 where <code>gutterMarkers</code> is an object mapping gutter IDs
1249 to marker elements, and <code>widgets</code> is an array
1250 of <a href="#addLineWidget">line widgets</a> attached to this
1251 line, and the various class properties refer to classes added
1252 with <a href="#addLineClass"><code>addLineClass</code></a>.</dd>
1254 <dt id="addWidget"><code><strong>cm.addWidget</strong>(pos: {line, ch}, node: Element, scrollIntoView: boolean)</code></dt>
1255 <dd>Puts <code>node</code>, which should be an absolutely
1256 positioned DOM node, into the editor, positioned right below the
1257 given <code>{line, ch}</code> position.
1258 When <code>scrollIntoView</code> is true, the editor will ensure
1259 that the entire node is visible (if possible). To remove the
1260 widget again, simply use DOM methods (move it somewhere else, or
1261 call <code>removeChild</code> on its parent).</dd>
1263 <dt id="addLineWidget"><code><strong>cm.addLineWidget</strong>(line: integer|LineHandle, node: Element, ?options: object) → LineWidget</code></dt>
1264 <dd>Adds a line widget, an element shown below a line, spanning
1265 the whole of the editor's width, and moving the lines below it
1266 downwards. <code>line</code> should be either an integer or a
1267 line handle, and <code>node</code> should be a DOM node, which
1268 will be displayed below the given line. <code>options</code>,
1269 when given, should be an object that configures the behavior of
1270 the widget. The following options are supported (all default to
1273 <dt><code><strong>coverGutter</strong>: boolean</code></dt>
1274 <dd>Whether the widget should cover the gutter.</dd>
1275 <dt><code><strong>noHScroll</strong>: boolean</code></dt>
1276 <dd>Whether the widget should stay fixed in the face of
1277 horizontal scrolling.</dd>
1278 <dt><code><strong>above</strong>: boolean</code></dt>
1279 <dd>Causes the widget to be placed above instead of below
1280 the text of the line.</dd>
1281 <dt><code><strong>showIfHidden</strong>: boolean</code></dt>
1282 <dd>When true, will cause the widget to be rendered even if
1283 the line it is associated with is hidden.</dd>
1284 <dt><code><strong>handleMouseEvents</strong>: boolean</code></dt>
1285 <dd>Determines whether the editor will capture mouse and
1286 drag events occurring in this widget. Default is false—the
1287 events will be left alone for the default browser handler,
1288 or specific handlers on the widget, to capture.</dd>
1289 <dt><code><strong>insertAt</strong>: integer</code></dt>
1290 <dd>By default, the widget is added below other widgets for
1291 the line. This option can be used to place it at a different
1292 position (zero for the top, N to put it after the Nth other
1293 widget). Note that this only has effect once, when the
1296 Note that the widget node will become a descendant of nodes with
1297 CodeMirror-specific CSS classes, and those classes might in some
1298 cases affect it. This method returns an object that represents
1299 the widget placement. It'll have a <code>line</code> property
1300 pointing at the line handle that it is associated with, and the following methods:
1302 <dt id="widget_clear"><code><strong>clear</strong>()</code></dt><dd>Removes the widget.</dd>
1303 <dt id="widget_changed"><code><strong>changed</strong>()</code></dt><dd>Call
1304 this if you made some change to the widget's DOM node that
1305 might affect its height. It'll force CodeMirror to update
1306 the height of the line that contains the widget.</dd>
1311 <h3 id="api_sizing">Sizing, scrolling and positioning methods</h3>
1314 <dt id="setSize"><code><strong>cm.setSize</strong>(width: number|string, height: number|string)</code></dt>
1315 <dd>Programatically set the size of the editor (overriding the
1316 applicable <a href="#css-resize">CSS
1317 rules</a>). <code>width</code> and <code>height</code> height
1318 can be either numbers (interpreted as pixels) or CSS units
1319 (<code>"100%"</code>, for example). You can
1320 pass <code>null</code> for either of them to indicate that that
1321 dimension should not be changed.</dd>
1323 <dt id="scrollTo"><code><strong>cm.scrollTo</strong>(x: number, y: number)</code></dt>
1324 <dd>Scroll the editor to a given (pixel) position. Both
1325 arguments may be left as <code>null</code>
1326 or <code>undefined</code> to have no effect.</dd>
1327 <dt id="getScrollInfo"><code><strong>cm.getScrollInfo</strong>() → {left, top, width, height, clientWidth, clientHeight}</code></dt>
1328 <dd>Get an <code>{left, top, width, height, clientWidth,
1329 clientHeight}</code> object that represents the current scroll
1330 position, the size of the scrollable area, and the size of the
1331 visible area (minus scrollbars).</dd>
1332 <dt id="scrollIntoView"><code><strong>cm.scrollIntoView</strong>(pos: {line, ch}|{left, top, right, bottom}, ?margin: number)</code></dt>
1333 <dd>Scrolls the given element into view. <code>pos</code> may be
1334 either a <code>{line, ch}</code> position, referring to a given
1335 character, <code>null</code>, to refer to the cursor, or
1336 a <code>{left, top, right, bottom}</code> object, in
1337 editor-local coordinates. The <code>margin</code> parameter is
1338 optional. When given, it indicates the amount of pixels around
1339 the given area that should be made visible as well.</dd>
1341 <dt id="cursorCoords"><code><strong>cm.cursorCoords</strong>(where: boolean|{line, ch}, mode: string) → {left, top, bottom}</code></dt>
1342 <dd>Returns an <code>{left, top, bottom}</code> object
1343 containing the coordinates of the cursor position.
1344 If <code>mode</code> is <code>"local"</code>, they will be
1345 relative to the top-left corner of the editable document. If it
1346 is <code>"page"</code> or not given, they are relative to the
1347 top-left corner of the page. <code>where</code> can be a boolean
1348 indicating whether you want the start (<code>true</code>) or the
1349 end (<code>false</code>) of the selection, or, if a <code>{line,
1350 ch}</code> object is given, it specifies the precise position at
1351 which you want to measure.</dd>
1352 <dt id="charCoords"><code><strong>cm.charCoords</strong>(pos: {line, ch}, ?mode: string) → {left, right, top, bottom}</code></dt>
1353 <dd>Returns the position and dimensions of an arbitrary
1354 character. <code>pos</code> should be a <code>{line, ch}</code>
1355 object. This differs from <code>cursorCoords</code> in that
1356 it'll give the size of the whole character, rather than just the
1357 position that the cursor would have when it would sit at that
1359 <dt id="coordsChar"><code><strong>cm.coordsChar</strong>(object: {left, top}, ?mode: string) → {line, ch}</code></dt>
1360 <dd>Given an <code>{left, top}</code> object, returns
1361 the <code>{line, ch}</code> position that corresponds to it. The
1362 optional <code>mode</code> parameter determines relative to what
1363 the coordinates are interpreted. It may
1364 be <code>"window"</code>, <code>"page"</code> (the default),
1365 or <code>"local"</code>.</dd>
1366 <dt id="lineAtHeight"><code><strong>cm.lineAtHeight</strong>(height: number, ?mode: string) → number</code></dt>
1367 <dd>Computes the line at the given pixel
1368 height. <code>mode</code> can be one of the same strings
1369 that <a href="#coordsChar"><code>coordsChar</code></a>
1371 <dt id="heightAtLine"><code><strong>cm.heightAtLine</strong>(line: number, ?mode: string) → number</code></dt>
1372 <dd>Computes the height of the top of a line, in the coordinate
1373 system specified by <code>mode</code>
1374 (see <a href="#coordsChar"><code>coordsChar</code></a>), which
1375 defaults to <code>"page"</code>. When a line below the bottom of
1376 the document is specified, the returned value is the bottom of
1377 the last line in the document.</dd>
1378 <dt id="defaultTextHeight"><code><strong>cm.defaultTextHeight</strong>() → number</code></dt>
1379 <dd>Returns the line height of the default font for the editor.</dd>
1380 <dt id="defaultCharWidth"><code><strong>cm.defaultCharWidth</strong>() → number</code></dt>
1381 <dd>Returns the pixel width of an 'x' in the default font for
1382 the editor. (Note that for non-monospace fonts, this is mostly
1383 useless, and even for monospace fonts, non-ascii characters
1384 might have a different width).</dd>
1386 <dt id="getViewport"><code><strong>cm.getViewport</strong>() → {from: number, to: number}</code></dt>
1387 <dd>Returns a <code>{from, to}</code> object indicating the
1388 start (inclusive) and end (exclusive) of the currently rendered
1389 part of the document. In big documents, when most content is
1390 scrolled out of view, CodeMirror will only render the visible
1391 part, and a margin around it. See also
1392 the <a href="#event_viewportChange"><code>viewportChange</code></a>
1395 <dt id="refresh"><code><strong>cm.refresh</strong>()</code></dt>
1396 <dd>If your code does something to change the size of the editor
1397 element (window resizes are already listened for), or unhides
1398 it, you should probably follow up by calling this method to
1399 ensure CodeMirror is still looking as intended.</dd>
1402 <h3 id="api_mode">Mode, state, and token-related methods</h3>
1404 <p>When writing language-aware functionality, it can often be
1405 useful to hook into the knowledge that the CodeMirror language
1406 mode has. See <a href="#modeapi">the section on modes</a> for a
1407 more detailed description of how these work.</p>
1410 <dt id="getMode"><code><strong>doc.getMode</strong>() → object</code></dt>
1411 <dd>Gets the (outer) mode object for the editor. Note that this
1412 is distinct from <code>getOption("mode")</code>, which gives you
1413 the mode specification, rather than the resolved, instantiated
1414 <a href="#defineMode">mode object</a>.</dd>
1416 <dt id="getModeAt"><code><strong>doc.getModeAt</strong>(pos: {line, ch}) → object</code></dt>
1417 <dd>Gets the inner mode at a given position. This will return
1418 the same as <a href="#getMode"><code>getMode</code></a> for
1419 simple modes, but will return an inner mode for nesting modes
1420 (such as <code>htmlmixed</code>).</dd>
1422 <dt id="getTokenAt"><code><strong>cm.getTokenAt</strong>(pos: {line, ch}, ?precise: boolean) → object</code></dt>
1423 <dd>Retrieves information about the token the current mode found
1424 before the given position (a <code>{line, ch}</code> object). The
1425 returned object has the following properties:
1427 <dt><code><strong>start</strong></code></dt><dd>The character (on the given line) at which the token starts.</dd>
1428 <dt><code><strong>end</strong></code></dt><dd>The character at which the token ends.</dd>
1429 <dt><code><strong>string</strong></code></dt><dd>The token's string.</dd>
1430 <dt><code><strong>type</strong></code></dt><dd>The token type the mode assigned
1431 to the token, such as <code>"keyword"</code>
1432 or <code>"comment"</code> (may also be null).</dd>
1433 <dt><code><strong>state</strong></code></dt><dd>The mode's state at the end of this token.</dd>
1435 If <code>precise</code> is true, the token will be guaranteed to be accurate based on recent edits. If false or
1436 not specified, the token will use cached state information, which will be faster but might not be accurate if
1437 edits were recently made and highlighting has not yet completed.
1440 <dt id="getTokenTypeAt"><code><strong>cm.getTokenTypeAt</strong>(pos: {line, ch}) → string</code></dt>
1441 <dd>This is a (much) cheaper version
1442 of <a href="#getTokenAt"><code>getTokenAt</code></a> useful for
1443 when you just need the type of the token at a given position,
1444 and no other information. Will return <code>null</code> for
1445 unstyled tokens, and a string, potentially containing multiple
1446 space-separated style names, otherwise.</dd>
1448 <dt id="getHelper"><code><strong>cm.getHelper</strong>(pos: {line, ch}, type: string) → helper</code></dt>
1449 <dd>Fetch appropriate helper for the given position. Helpers
1450 provide a way to look up functionality appropriate for a mode.
1451 The <code>type</code> argument provides the helper namespace
1453 also <a href="#registerHelper"><code>registerHelper</code></a>),
1454 in which the value will be looked up. The key that is used
1455 depends on the <a href="#getMode">mode</a> active at the given
1456 position. If the mode object contains a property with the same
1457 name as the <code>type</code> argument, that is tried first.
1458 Next, the mode's <code>helperType</code>, if any, is tried. And
1459 finally, the mode's name.</dd>
1461 <dt id="getStateAfter"><code><strong>cm.getStateAfter</strong>(?line: integer, ?precise: boolean) → object</code></dt>
1462 <dd>Returns the mode's parser state, if any, at the end of the
1463 given line number. If no line number is given, the state at the
1464 end of the document is returned. This can be useful for storing
1465 parsing errors in the state, or getting other kinds of
1466 contextual information for a line. <code>precise</code> is defined
1467 as in <code>getTokenAt()</code>.</dd>
1470 <h3 id="api_misc">Miscellaneous methods</h3>
1473 <dt id="operation"><code><strong>cm.operation</strong>(func: () → any) → any</code></dt>
1474 <dd>CodeMirror internally buffers changes and only updates its
1475 DOM structure after it has finished performing some operation.
1476 If you need to perform a lot of operations on a CodeMirror
1477 instance, you can call this method with a function argument. It
1478 will call the function, buffering up all changes, and only doing
1479 the expensive update after the function returns. This can be a
1480 lot faster. The return value from this method will be the return
1481 value of your function.</dd>
1483 <dt id="indentLine"><code><strong>cm.indentLine</strong>(line: integer, ?dir: string|integer)</code></dt>
1484 <dd>Adjust the indentation of the given line. The second
1485 argument (which defaults to <code>"smart"</code>) may be one of:
1487 <dt><code><strong>"prev"</strong></code></dt>
1488 <dd>Base indentation on the indentation of the previous line.</dd>
1489 <dt><code><strong>"smart"</strong></code></dt>
1490 <dd>Use the mode's smart indentation if available, behave
1491 like <code>"prev"</code> otherwise.</dd>
1492 <dt><code><strong>"add"</strong></code></dt>
1493 <dd>Increase the indentation of the line by
1494 one <a href="#option_indentUnit">indent unit</a>.</dd>
1495 <dt><code><strong>"subtract"</strong></code></dt>
1496 <dd>Reduce the indentation of the line.</dd>
1497 <dt><code><strong><integer></strong></code></dt>
1498 <dd>Add (positive number) or reduce (negative number) the
1499 indentation by the given amount of spaces.</dd>
1502 <dt id="toggleOverwrite"><code><strong>cm.toggleOverwrite</strong>(?value: bool)</code></dt>
1503 <dd>Switches between overwrite and normal insert mode (when not
1504 given an argument), or sets the overwrite mode to a specific
1505 state (when given an argument).</dd>
1507 <dt id="posFromIndex"><code><strong>doc.posFromIndex</strong>(index: integer) → {line, ch}</code></dt>
1508 <dd>Calculates and returns a <code>{line, ch}</code> object for a
1509 zero-based <code>index</code> who's value is relative to the start of the
1510 editor's text. If the <code>index</code> is out of range of the text then
1511 the returned object is clipped to start or end of the text
1513 <dt id="indexFromPos"><code><strong>doc.indexFromPos</strong>(object: {line, ch}) → integer</code></dt>
1514 <dd>The reverse of <a href="#posFromIndex"><code>posFromIndex</code></a>.</dd>
1516 <dt id="focus"><code><strong>cm.focus</strong>()</code></dt>
1517 <dd>Give the editor focus.</dd>
1519 <dt id="getInputField"><code><strong>cm.getInputField</strong>() → TextAreaElement</code></dt>
1520 <dd>Returns the hidden textarea used to read input.</dd>
1521 <dt id="getWrapperElement"><code><strong>cm.getWrapperElement</strong>() → Element</code></dt>
1522 <dd>Returns the DOM node that represents the editor, and
1523 controls its size. Remove this from your tree to delete an
1524 editor instance.</dd>
1525 <dt id="getScrollerElement"><code><strong>cm.getScrollerElement</strong>() → Element</code></dt>
1526 <dd>Returns the DOM node that is responsible for the scrolling
1528 <dt id="getGutterElement"><code><strong>cm.getGutterElement</strong>() → Element</code></dt>
1529 <dd>Fetches the DOM node that contains the editor gutters.</dd>
1532 <h3 id="api_static">Static properties</h3>
1533 <p>The <code>CodeMirror</code> object itself provides
1534 several useful properties.</p>
1537 <dt id="version"><code><strong>CodeMirror.version</strong>: string</code></dt>
1538 <dd>It contains a string that indicates the version of the
1539 library. This is a triple of
1540 integers <code>"major.minor.patch"</code>,
1541 where <code>patch</code> is zero for releases, and something
1542 else (usually one) for dev snapshots.</dd>
1544 <dt id="fromTextArea"><code><strong>CodeMirror.fromTextArea</strong>(textArea: TextAreaElement, ?config: object)</code></dt>
1546 The method provides another way to initialize an editor. It takes a
1547 textarea DOM node as first argument and an optional configuration
1548 object as second. It will replace the textarea with a CodeMirror
1549 instance, and wire up the form of that textarea (if any) to make
1550 sure the editor contents are put into the textarea when the form
1551 is submitted. A CodeMirror instance created this way has three
1554 <dt id="save"><code><strong>cm.save</strong>()</code></dt>
1555 <dd>Copy the content of the editor into the textarea.</dd>
1557 <dt id="toTextArea"><code><strong>cm.toTextArea</strong>()</code></dt>
1558 <dd>Remove the editor, and restore the original textarea (with
1559 the editor's current content).</dd>
1561 <dt id="getTextArea"><code><strong>cm.getTextArea</strong>() → TextAreaElement</code></dt>
1562 <dd>Returns the textarea that the instance was based on.</dd>
1566 <dt id="defaults"><code><strong>CodeMirror.defaults</strong>: object</code></dt>
1567 <dd>An object containing default values for
1568 all <a href="#config">options</a>. You can assign to its
1569 properties to modify defaults (though this won't affect editors
1570 that have already been created).</dd>
1572 <dt id="defineExtension"><code><strong>CodeMirror.defineExtension</strong>(name: string, value: any)</code></dt>
1573 <dd>If you want to define extra methods in terms of the
1574 CodeMirror API, it is possible to
1575 use <code>defineExtension</code>. This will cause the given
1576 value (usually a method) to be added to all CodeMirror instances
1577 created from then on.</dd>
1579 <dt id="defineDocExtension"><code><strong>CodeMirror.defineDocExtension</strong>(name: string, value: any)</code></dt>
1580 <dd>Like <a href="#defineExtenstion"><code>defineExtension</code></a>,
1581 but the method will be added to the interface
1582 for <a href="#Doc"><code>Doc</code></a> objects instead.</dd>
1584 <dt id="defineOption"><code><strong>CodeMirror.defineOption</strong>(name: string,
1585 default: any, updateFunc: function)</code></dt>
1586 <dd>Similarly, <code>defineOption</code> can be used to define new options for
1587 CodeMirror. The <code>updateFunc</code> will be called with the
1588 editor instance and the new value when an editor is initialized,
1589 and whenever the option is modified
1590 through <a href="#setOption"><code>setOption</code></a>.</dd>
1592 <dt id="defineInitHook"><code><strong>CodeMirror.defineInitHook</strong>(func: function)</code></dt>
1593 <dd>If your extention just needs to run some
1594 code whenever a CodeMirror instance is initialized,
1595 use <code>CodeMirror.defineInitHook</code>. Give it a function as
1596 its only argument, and from then on, that function will be called
1597 (with the instance as argument) whenever a new CodeMirror instance
1598 is initialized.</dd>
1600 <dt id="registerHelper"><code><strong>CodeMirror.registerHelper</strong>(type: string, name: string, value: helper)</code></dt>
1601 <dd>Registers a helper value with the given <code>name</code> in
1602 the given namespace (<code>type</code>). This is used to define
1603 functionality that may be looked up by mode. Will create (if it
1604 doesn't already exist) a property on the <code>CodeMirror</code>
1605 object for the given <code>type</code>, pointing to an object
1606 that maps names to values. I.e. after
1607 doing <code>CodeMirror.registerHelper("hint", "foo",
1608 myFoo)</code>, the value <code>CodeMirror.hint.foo</code> will
1609 point to <code>myFoo</code>.</dd>
1611 <dt id="Pos"><code><strong>CodeMirror.Pos</strong>(line: integer, ?ch: integer)</code></dt>
1612 <dd>A constructor for the <code>{line, ch}</code> objects that
1613 are used to represent positions in editor documents.</dd>
1615 <dt id="changeEnd"><code><strong>CodeMirror.changeEnd</strong>(change: object) → {line, ch}</code></dt>
1616 <dd>Utility function that computes an end position from a change
1617 (an object with <code>from</code>, <code>to</code>,
1618 and <code>text</code> properties, as passed to
1619 various <a href="#event_change">event handlers</a>). The
1620 returned position will be the end of the changed
1621 range, <em>after</em> the change is applied.</dd>
1624 <h2 id="addons">Addons</h2>
1626 <p>The <code>addon</code> directory in the distribution contains a
1627 number of reusable components that implement extra editor
1628 functionality. In brief, they are:</p>
1631 <dt id="addon_dialog"><a href="../addon/dialog/dialog.js"><code>dialog/dialog.js</code></a></dt>
1632 <dd>Provides a very simple way to query users for text input.
1633 Adds an <code>openDialog</code> method to CodeMirror instances,
1634 which can be called with an HTML fragment that provides the
1635 prompt (should include an <code>input</code> tag), and a
1636 callback function that is called when text has been entered.
1637 Depends on <code>addon/dialog/dialog.css</code>.</dd>
1639 <dt id="addon_searchcursor"><a href="../addon/search/searchcursor.js"><code>search/searchcursor.js</code></a></dt>
1640 <dd>Adds the <code>getSearchCursor(query, start, caseFold) →
1641 cursor</code> method to CodeMirror instances, which can be used
1642 to implement search/replace functionality. <code>query</code>
1643 can be a regular expression or a string (only strings will match
1644 across lines—if they contain newlines). <code>start</code>
1645 provides the starting position of the search. It can be
1646 a <code>{line, ch}</code> object, or can be left off to default
1647 to the start of the document. <code>caseFold</code> is only
1648 relevant when matching a string. It will cause the search to be
1649 case-insensitive. A search cursor has the following methods:
1651 <dt><code><strong>findNext</strong>() → boolean</code></dt>
1652 <dt><code><strong>findPrevious</strong>() → boolean</code></dt>
1653 <dd>Search forward or backward from the current position.
1654 The return value indicates whether a match was found. If
1655 matching a regular expression, the return value will be the
1656 array returned by the <code>match</code> method, in case you
1657 want to extract matched groups.</dd>
1658 <dt><code><strong>from</strong>() → {line, ch}</code></dt>
1659 <dt><code><strong>to</strong>() → {line, ch}</code></dt>
1660 <dd>These are only valid when the last call
1661 to <code>findNext</code> or <code>findPrevious</code> did
1662 not return false. They will return <code>{line, ch}</code>
1663 objects pointing at the start and end of the match.</dd>
1664 <dt><code><strong>replace</strong>(text: string)</code></dt>
1665 <dd>Replaces the currently found match with the given text
1666 and adjusts the cursor position to reflect the
1670 <dt id="addon_search"><a href="../addon/search/search.js"><code>search/search.js</code></a></dt>
1671 <dd>Implements the search commands. CodeMirror has keys bound to
1672 these by default, but will not do anything with them unless an
1673 implementation is provided. Depends
1674 on <code>searchcursor.js</code>, and will make use
1675 of <a href="#addon_dialog"><code>openDialog</code></a> when
1676 available to make prompting for search queries less ugly.</dd>
1678 <dt id="addon_matchbrackets"><a href="../addon/edit/matchbrackets.js"><code>edit/matchbrackets.js</code></a></dt>
1679 <dd>Defines an option <code>matchBrackets</code> which, when set
1680 to true, causes matching brackets to be highlighted whenever the
1681 cursor is next to them. It also adds a
1682 method <code>matchBrackets</code> that forces this to happen
1683 once, and a method <code>findMatchingBracket</code> that can be
1684 used to run the bracket-finding algorithm that this uses
1687 <dt id="addon_closebrackets"><a href="../addon/edit/closebrackets.js"><code>edit/closebrackets.js</code></a></dt>
1688 <dd>Defines an option <code>autoCloseBrackets</code> that will
1689 auto-close brackets and quotes when typed. By default, it'll
1690 auto-close <code>()[]{}''""</code>, but you can pass it a string
1691 similar to that (containing pairs of matching characters), or an
1692 object with <code>pairs</code> and
1693 optionally <code>explode</code> properties to customize
1694 it. <code>explode</code> should be a similar string that gives
1695 the pairs of characters that, when enter is pressed between
1696 them, should have the second character also moved to its own
1697 line. <a href="../demo/closebrackets.html">Demo here</a>.</dd>
1699 <dt id="addon_matchtags"><a href="../addon/edit/matchtags.js"><code>edit/matchtags.js</code></a></dt>
1700 <dd>Defines an option <code>matchTags</code> that, when enabled,
1701 will cause the tags around the cursor to be highlighted (using
1702 the <code>CodeMirror-matchingbrackets</code> class). Also
1704 a <a href="#commands">command</a> <code>toMatchingTag</code>,
1705 which you can bind a key to in order to jump to the tag mathing
1706 the one under the cursor. Depends on
1707 the <code>addon/fold/xml-fold.js</code>
1708 addon. <a href="../demo/matchtags.html">Demo here.</a></dd>
1710 <dt id="addon_trailingspace"><a href="../addon/edit/trailingspace.js"><code>edit/trailingspace.js</code></a></dt>
1711 <dd>Adds an option <code>showTrailingSpace</code> which, when
1712 enabled, adds the CSS class <code>cm-trailingspace</code> to
1713 stretches of whitespace at the end of lines.
1714 The <a href="../demo/trailingspace.html">demo</a> has a nice
1715 squiggly underline style for this class.</dd>
1717 <dt id="addon_comment"><a href="../addon/comment/comment.js"><code>comment/comment.js</code></a></dt>
1718 <dd>Addon for commenting and uncommenting code. Adds three
1719 methods to CodeMirror instances:
1721 <dt id="lineComment"><code><strong>lineComment</strong>(from: {line, ch}, to: {line, ch}, ?options: object)</code></dt>
1722 <dd>Set the lines in the given range to be line comments. Will
1723 fall back to <code>blockComment</code> when no line comment
1724 style is defined for the mode.</dd>
1725 <dt id="blockComment"><code><strong>blockComment</strong>(from: {line, ch}, to: {line, ch}, ?options: object)</code></dt>
1726 <dd>Wrap the code in the given range in a block comment. Will
1727 fall back to <code>lineComment</code> when no block comment
1728 style is defined for the mode.</dd>
1729 <dt id="uncomment"><code><strong>uncomment</strong>(from: {line, ch}, to: {line, ch}, ?options: object) → boolean</code></dt>
1730 <dd>Try to uncomment the given range.
1731 Returns <code>true</code> if a comment range was found and
1732 removed, <code>false</code> otherwise.</dd>
1734 The <code>options</code> object accepted by these methods may
1735 have the following properties:
1737 <dt><code>blockCommentStart, blockCommentEnd, blockCommentLead, lineComment: string</code></dt>
1738 <dd>Override the <a href="#mode_comment">comment string
1739 properties</a> of the mode with custom comment strings.</dd>
1740 <dt><code><strong>padding</strong>: string</code></dt>
1741 <dd>A string that will be inserted after opening and before
1742 closing comment markers. Defaults to a single space.</dd>
1743 <dt><code><strong>commentBlankLines</strong>: boolean</code></dt>
1744 <dd>Whether, when adding line comments, to also comment lines
1745 that contain only whitespace.</dd>
1746 <dt><code><strong>indent</strong>: boolean</code></dt>
1747 <dd>When adding line comments and this is turned on, it will
1748 align the comment block to the current indentation of the
1749 first line of the block.</dd>
1750 <dt><code><strong>fullLines</strong>: boolean</code></dt>
1751 <dd>When block commenting, this controls whether the whole
1752 lines are indented, or only the precise range that is given.
1753 Defaults to <code>true</code>.</dd>
1755 The addon also defines
1756 a <code>toggleComment</code> <a href="#commands">command</a>,
1757 which will try to uncomment the current selection, and if that
1758 fails, line-comments it.</dd>
1760 <dt id="addon_foldcode"><a href="../addon/fold/foldcode.js"><code>fold/foldcode.js</code></a></dt>
1761 <dd>Helps with code folding. Adds a <code>foldCode</code> method
1762 to editor instances, which will try to do a code fold starting
1763 at the given line, or unfold the fold that is already present.
1764 The method takes as first argument the position that should be
1765 folded (may be a line number or
1766 a <a href="#Pos"><code>Pos</code></a>), and as second optional
1767 argument either a range-finder function, or an options object,
1768 supporting the following properties:
1770 <dt><code><strong>rangeFinder</strong>: fn(CodeMirror, Pos)</code></dt>
1771 <dd>The function that is used to find foldable ranges. If this
1772 is not directly passed, it will
1773 call <a href="#getHelper"><code>getHelper</code></a> with
1774 a <code>"fold"</code> type to find one that's appropriate for
1775 the mode. There are files in
1776 the <a href="../addon/fold/"><code>addon/fold/</code></a>
1777 directory providing <code>CodeMirror.fold.brace</code>, which
1778 finds blocks in brace languages (JavaScript, C, Java,
1779 etc), <code>CodeMirror.fold.indent</code>, for languages where
1780 indentation determines block structure (Python, Haskell),
1781 and <code>CodeMirror.fold.xml</code>, for XML-style
1783 <dt><code><strong>widget</strong>: string|Element</code></dt>
1784 <dd>The widget to show for folded ranges. Can be either a
1785 string, in which case it'll become a span with
1786 class <code>CodeMirror-foldmarker</code>, or a DOM node.</dd>
1787 <dt><code><strong>scanUp</strong>: boolean</code></dt>
1788 <dd>When true (default is false), the addon will try to find
1789 foldable ranges on the lines above the current one if there
1790 isn't an eligible one on the given line.</dd>
1791 <dt><code><strong>minFoldSize</strong>: integer</code></dt>
1792 <dd>The minimum amount of lines that a fold should span to be
1793 accepted. Defaults to 0, which also allows single-line
1796 See <a href="../demo/folding.html">the demo</a> for an
1799 <dt id="addon_foldgutter"><a href="../addon/fold/foldgutter.js"><code>fold/foldgutter.js</code></a></dt>
1800 <dd>Provides an option <code>foldGutter</code>, which can be
1801 used to create a gutter with markers indicating the blocks that
1802 can be folded. Create a gutter using
1803 the <a href="#option_gutters"><code>gutters</code></a> option,
1804 giving it the class <code>CodeMirror-foldgutter</code> or
1805 something else if you configure the addon to use a different
1806 class, and this addon will show markers next to folded and
1807 foldable blocks, and handle clicks in this gutter. The option
1808 can be either set to <code>true</code>, or an object containing
1809 the following optional option fields:
1811 <dt><code><strong>gutter</strong>: string</code></dt>
1812 <dd>The CSS class of the gutter. Defaults
1813 to <code>"CodeMirror-foldgutter"</code>. You will have to
1814 style this yourself to give it a width (and possibly a
1816 <dt><code><strong>indicatorOpen</strong>: string | Element</code></dt>
1817 <dd>A CSS class or DOM element to be used as the marker for
1818 open, foldable blocks. Defaults
1819 to <code>"CodeMirror-foldgutter-open"</code>.</dd>
1820 <dt><code><strong>indicatorFolded</strong>: string | Element</code></dt>
1821 <dd>A CSS class or DOM element to be used as the marker for
1822 folded blocks. Defaults to <code>"CodeMirror-foldgutter-folded"</code>.</dd>
1823 <dt><code><strong>rangeFinder</strong>: fn(CodeMirror, Pos)</code></dt>
1824 <dd>The range-finder function to use when determining whether
1825 something can be folded. When not
1826 given, <a href="#getHelper"><code>getHelper</code></a> will be
1827 used to determine a default.</dd>
1829 Demo <a href="../demo/folding.html">here</a>.</dd>
1831 <dt id="addon_runmode"><a href="../addon/runmode/runmode.js"><code>runmode/runmode.js</code></a></dt>
1832 <dd>Can be used to run a CodeMirror mode over text without
1833 actually opening an editor instance.
1834 See <a href="../demo/runmode.html">the demo</a> for an example.
1835 There are alternate versions of the file avaible for
1836 running <a href="../addon/runmode/runmode-standalone.js">stand-alone</a>
1837 (without including all of CodeMirror) and
1838 for <a href="../addon/runmode/runmode.node.js">running under
1841 <dt id="addon_overlay"><a href="../addon/mode/overlay.js"><code>mode/overlay.js</code></a></dt>
1842 <dd>Mode combinator that can be used to extend a mode with an
1843 'overlay' — a secondary mode is run over the stream, along with
1844 the base mode, and can color specific pieces of text without
1845 interfering with the base mode.
1846 Defines <code>CodeMirror.overlayMode</code>, which is used to
1847 create such a mode. See <a href="../demo/mustache.html">this
1848 demo</a> for a detailed example.</dd>
1850 <dt id="addon_multiplex"><a href="../addon/mode/multiplex.js"><code>mode/multiplex.js</code></a></dt>
1851 <dd>Mode combinator that can be used to easily 'multiplex'
1852 between several modes.
1853 Defines <code>CodeMirror.multiplexingMode</code> which, when
1854 given as first argument a mode object, and as other arguments
1855 any number of <code>{open, close, mode [, delimStyle, innerStyle]}</code>
1856 objects, will return a mode object that starts parsing using the
1857 mode passed as first argument, but will switch to another mode
1858 as soon as it encounters a string that occurs in one of
1859 the <code>open</code> fields of the passed objects. When in a
1860 sub-mode, it will go back to the top mode again when
1861 the <code>close</code> string is encountered.
1862 Pass <code>"\n"</code> for <code>open</code> or <code>close</code>
1863 if you want to switch on a blank line.
1864 <ul><li>When <code>delimStyle</code> is specified, it will be the token
1865 style returned for the delimiter tokens.</li>
1866 <li>When <code>innerStyle</code> is specified, it will be the token
1867 style added for each inner mode token.</li></ul>
1868 The outer mode will not see the content between the delimiters.
1869 See <a href="../demo/multiplex.html">this demo</a> for an
1872 <dt id="addon_show-hint"><a href="../addon/hint/show-hint.js"><code>hint/show-hint.js</code></a></dt>
1873 <dd>Provides a framework for showing autocompletion hints.
1874 Defines <code>CodeMirror.showHint</code>, which takes a
1875 CodeMirror instance, a hinting function, and optionally an
1876 options object, and pops up a widget that allows the user to
1877 select a completion. Hinting functions are function that take an
1878 editor instance and an optional options object, and return
1879 a <code>{list, from, to}</code> object, where <code>list</code>
1880 is an array of strings or objects (the completions),
1881 and <code>from</code> and <code>to</code> give the start and end
1882 of the token that is being completed as <code>{line, ch}</code>
1883 objects. If no hinting function is given, the addon will try to
1884 use <a href="#getHelper"><code>getHelper</code></a> with
1885 the <code>"hint"</code> type to find one. When completions
1886 aren't simple strings, they should be objects with the folowing
1889 <dt><code><strong>text</strong>: string</code></dt>
1890 <dd>The completion text. This is the only required
1892 <dt><code><strong>displayText</strong>: string</code></dt>
1893 <dd>The text that should be displayed in the menu.</dd>
1894 <dt><code><strong>className</strong>: string</code></dt>
1895 <dd>A CSS class name to apply to the completion's line in the
1897 <dt><code><strong>render</strong>: fn(Element, self, data)</code></dt>
1898 <dd>A method used to create the DOM structure for showing the
1899 completion by appending it to its first argument.</dd>
1900 <dt><code><strong>hint</strong>: fn(CodeMirror, self, data)</code></dt>
1901 <dd>A method used to actually apply the completion, instead of
1902 the default behavior.</dd>
1904 The plugin understands the following options (the options object
1905 will also be passed along to the hinting function, which may
1906 understand additional options):
1908 <dt><code><strong>async</strong>: boolean</code></dt>
1909 <dd>When set to true, the hinting function's signature should
1910 be <code>(cm, callback, ?options)</code>, and the completion
1911 interface will only be popped up when the hinting function
1912 calls the callback, passing it the object holding the
1914 <dt><code><strong>completeSingle</strong>: boolean</code></dt>
1915 <dd>Determines whether, when only a single completion is
1916 available, it is completed without showing the dialog.
1917 Defaults to true.</dd>
1918 <dt><code><strong>alignWithWord</strong>: boolean</code></dt>
1919 <dd>Whether the pop-up should be horizontally aligned with the
1920 start of the word (true, default), or with the cursor (false).</dd>
1921 <dt><code><strong>closeOnUnfocus</strong>: boolean</code></dt>
1922 <dd>When enabled (which is the default), the pop-up will close
1923 when the editor is unfocused.</dd>
1924 <dt><code><strong>customKeys</strong>: keymap</code></dt>
1925 <dd>Allows you to provide a custom keymap of keys to be active
1926 when the pop-up is active. The handlers will be called with an
1927 extra argument, a handle to the completion menu, which
1928 has <code>moveFocus(n)</code>, <code>setFocus(n)</code>, <code>pick()</code>,
1929 and <code>close()</code> methods (see the source for details),
1930 that can be used to change the focused element, pick the
1931 current element or close the menu.</dd>
1932 <dt><code><strong>extraKeys</strong>: keymap</code></dt>
1933 <dd>Like <code>customKeys</code> above, but the bindings will
1934 be added to the set of default bindings, instead of replacing
1937 The following events will be fired on the completions object
1940 <dt><code><strong>"shown"</strong> ()</code></dt>
1941 <dd>Fired when the pop-up is shown.</dd>
1942 <dt><code><strong>"select"</strong> (completion, Element)</code></dt>
1943 <dd>Fired when a completion is selected. Passed the completion
1944 value (string or object) and the DOM node that represents it
1946 <dt><code><strong>"close"</strong> ()</code></dt>
1947 <dd>Fired when the completion is finished.</dd>
1949 This addon depends styles
1950 from <code>addon/hint/show-hint.css</code>. Check
1951 out <a href="../demo/complete.html">the demo</a> for an
1954 <dt id="addon_javascript-hint"><a href="../addon/hint/javascript-hint.js"><code>hint/javascript-hint.js</code></a></dt>
1955 <dd>Defines a simple hinting function for JavaScript
1956 (<code>CodeMirror.hint.javascript</code>) and CoffeeScript
1957 (<code>CodeMirror.hint.coffeescript</code>) code. This will
1958 simply use the JavaScript environment that the editor runs in as
1959 a source of information about objects and their properties.</dd>
1961 <dt id="addon_xml-hint"><a href="../addon/hint/xml-hint.js"><code>hint/xml-hint.js</code></a></dt>
1962 <dd>Defines <code>CodeMirror.hint.xml</code>, which produces
1963 hints for XML tagnames, attribute names, and attribute values,
1964 guided by a <code>schemaInfo</code> option (a property of the
1965 second argument passed to the hinting function, or the third
1966 argument passed to <code>CodeMirror.showHint</code>).<br>The
1967 schema info should be an object mapping tag names to information
1968 about these tags, with optionally a <code>"!top"</code> property
1969 containing a list of the names of valid top-level tags. The
1970 values of the properties should be objects with optional
1971 properties <code>children</code> (an array of valid child
1972 element names, omit to simply allow all tags to appear)
1973 and <code>attrs</code> (an object mapping attribute names
1974 to <code>null</code> for free-form attributes, and an array of
1975 valid values for restricted
1976 attributes). <a href="../demo/xmlcomplete.html">Demo
1979 <dt id="addon_html-hint"><a href="../addon/hint/html-hint.js"><code>hint/html-hint.js</code></a></dt>
1980 <dd>Provides schema info to
1981 the <a href="#addon_xml-hint">xml-hint</a> addon for HTML
1982 documents. Defines a schema
1983 object <code>CodeMirror.htmlSchema</code> that you can pass to
1984 as a <code>schemaInfo</code> option, and
1985 a <code>CodeMirror.hint.html</code> hinting function that
1986 automatically calls <code>CodeMirror.hint.xml</code> with this
1988 the <a href="../demo/html5complete.html">demo</a>.</dd>
1990 <dt id="addon_python-hint"><a href="../addon/hint/python-hint.js"><code>hint/python-hint.js</code></a></dt>
1991 <dd>A very simple hinting function for Python code.
1992 Defines <code>CodeMirror.hint.python</code>.</dd>
1994 <dt id="addon_anyword-hint"><a href="../addon/hint/anyword-hint.js"><code>hint/anyword-hint.js</code></a></dt>
1995 <dd>A very simple hinting function
1996 (<code>CodeMirror.hint.anyword</code>) that simply looks for
1997 words in the nearby code and completes to those. Takes two
1998 optional options, <code>word</code>, a regular expression that
1999 matches words (sequences of one or more character),
2000 and <code>range</code>, which defines how many lines the addon
2001 should scan when completing (defaults to 500).</dd>
2003 <dt id="addon_match-highlighter"><a href="../addon/search/match-highlighter.js"><code>match-highlighter.js</code></a></dt>
2004 <dd>Adds a <code>highlightSelectionMatches</code> option that
2005 can be enabled to highlight all instances of a currently
2006 selected word. Can be set either to true or to an object
2007 containing the following options: <code>minChars</code>, for the
2008 minimum amount of selected characters that triggers a highlight
2009 (default 2), <code>style</code>, for the style to be used to
2010 highlight the matches (default <code>"matchhighlight"</code>,
2011 which will correspond to CSS
2012 class <code>cm-matchhighlight</code>),
2013 and <code>showToken</code> which can be set to <code>true</code>
2014 or to a regexp matching the characters that make up a word. When
2015 enabled, it causes the current word to be highlighted when
2016 nothing is selected (defaults to off).
2017 Demo <a href="../demo/matchhighlighter.html">here</a>.</dd>
2019 <dt id="addon_lint"><a href="../addon/lint/lint.js"><code>lint/lint.js</code></a></dt>
2020 <dd>Defines an interface component for showing linting warnings,
2021 with pluggable warning sources
2022 (see <a href="../addon/lint/json-lint.js"><code>json-lint.js</code></a>
2023 and <a href="../addon/lint/javascript-lint.js"><code>javascript-lint.js</code></a>
2024 in the same directory). Defines a <code>lint</code> option that
2025 can be set to a warning source (for
2026 example <code>CodeMirror.lint.javascript</code>), or
2027 to <code>true</code>, in which
2028 case <a href="#getHelper"><code>getHelper</code></a> with
2029 type <code>"lint"</code> is used to determined a validator
2030 function. Depends on <code>addon/lint/lint.css</code>. A demo
2031 can be found <a href="../demo/lint.html">here</a>.</dd>
2033 <dt id="addon_mark-selection"><a href="../addon/selection/mark-selection.js"><code>selection/mark-selection.js</code></a></dt>
2034 <dd>Causes the selected text to be marked with the CSS class
2035 <code>CodeMirror-selectedtext</code> when the <code>styleSelectedText</code> option
2036 is enabled. Useful to change the colour of the selection (in addition to the background),
2037 like in <a href="../demo/markselection.html">this demo</a>.</dd>
2039 <dt id="addon_active-line"><a href="../addon/selection/active-line.js"><code>selection/active-line.js</code></a></dt>
2040 <dd>Defines a <code>styleActiveLine</code> option that, when enabled,
2041 gives the wrapper of the active line the class <code>CodeMirror-activeline</code>,
2042 and adds a background with the class <code>CodeMirror-activeline-background</code>.
2043 is enabled. See the <a href="../demo/activeline.html">demo</a>.</dd>
2045 <dt id="addon_closetag"><a href="../addon/edit/closetag.js"><code>edit/closetag.js</code></a></dt>
2046 <dd>Provides utility functions for adding automatic tag closing
2048 the <a href="../demo/closetag.html">demo</a>.</dd>
2050 <dt id="addon_loadmode"><a href="../addon/mode/loadmode.js"><code>mode/loadmode.js</code></a></dt>
2051 <dd>Defines a <code>CodeMirror.requireMode(modename,
2052 callback)</code> function that will try to load a given mode and
2053 call the callback when it succeeded. You'll have to
2054 set <code>CodeMirror.modeURL</code> to a string that mode paths
2055 can be constructed from, for
2056 example <code>"mode/%N/%N.js"</code>—the <code>%N</code>'s will
2057 be replaced with the mode name. Also
2058 defines <code>CodeMirror.autoLoadMode(instance, mode)</code>,
2059 which will ensure the given mode is loaded and cause the given
2060 editor instance to refresh its mode when the loading
2061 succeeded. See the <a href="../demo/loadmode.html">demo</a>.</dd>
2063 <dt id="addon_continuecomment"><a href="../addon/edit/continuecomment.js"><code>edit/continuecomment.js</code></a></dt>
2064 <dd>Adds an <code>continueComments</code> option, which can be
2065 set to true to have the editor prefix new lines inside C-like
2066 block comments with an asterisk when Enter is pressed. It can
2067 also be set to a string in order to bind this functionality to a
2070 <dt id="addon_placeholder"><a href="../addon/display/placeholder.js"><code>display/placeholder.js</code></a></dt>
2071 <dd>Adds a <code>placeholder</code> option that can be used to
2072 make text appear in the editor when it is empty and not focused.
2073 Also gives the editor a <code>CodeMirror-empty</code> CSS class
2074 whenever it doesn't contain any text.
2075 See <a href="../demo/placeholder.html">the demo</a>.</dd>
2077 <dt id="addon_merge"><a href="../addon/merge/merge.js"><code>merge/merge.js</code></a></dt>
2078 <dd>Implements an interface for merging changes, using either a
2079 2-way or a 3-way view. The <code>CodeMirror.MergeView</code>
2080 constructor takes arguments similar to
2081 the <a href="#CodeMirror"><code>CodeMirror</code></a>
2082 constructor, first a node to append the interface to, and then
2083 an options object. Two extra optional options are
2084 recognized, <code>origLeft</code> and <code>origRight</code>,
2085 which may be strings that provide original versions of the
2086 document, which will be shown to the left and right of the
2087 editor in non-editable CodeMirror instances. The merge interface
2088 will highlight changes between the editable document and the
2089 original(s) (<a href="../demo/merge.html">demo</a>).</dd>
2092 <h2 id="modeapi">Writing CodeMirror Modes</h2>
2094 <p>Modes typically consist of a single JavaScript file. This file
2095 defines, in the simplest case, a lexer (tokenizer) for your
2096 language—a function that takes a character stream as input,
2097 advances it past a token, and returns a style for that token. More
2098 advanced modes can also handle indentation for the language.</p>
2100 <p id="defineMode">The mode script should
2101 call <code>CodeMirror.defineMode</code> to register itself with
2102 CodeMirror. This function takes two arguments. The first should be
2103 the name of the mode, for which you should use a lowercase string,
2104 preferably one that is also the name of the files that define the
2105 mode (i.e. <code>"xml"</code> is defined in <code>xml.js</code>). The
2106 second argument should be a function that, given a CodeMirror
2107 configuration object (the thing passed to
2108 the <code>CodeMirror</code> function) and an optional mode
2109 configuration object (as in
2110 the <a href="#option_mode"><code>mode</code></a> option), returns
2113 <p>Typically, you should use this second argument
2114 to <code>defineMode</code> as your module scope function (modes
2115 should not leak anything into the global scope!), i.e. write your
2116 whole mode inside this function.</p>
2118 <p>The main responsibility of a mode script is <em>parsing</em>
2119 the content of the editor. Depending on the language and the
2120 amount of functionality desired, this can be done in really easy
2121 or extremely complicated ways. Some parsers can be stateless,
2122 meaning that they look at one element (<em>token</em>) of the code
2123 at a time, with no memory of what came before. Most, however, will
2124 need to remember something. This is done by using a <em>state
2125 object</em>, which is an object that is always passed when
2126 reading a token, and which can be mutated by the tokenizer.</p>
2128 <p id="startState">Modes that use a state must define
2129 a <code>startState</code> method on their mode object. This is a
2130 function of no arguments that produces a state object to be used
2131 at the start of a document.</p>
2133 <p id="token">The most important part of a mode object is
2134 its <code>token(stream, state)</code> method. All modes must
2135 define this method. It should read one token from the stream it is
2136 given as an argument, optionally update its state, and return a
2137 style string, or <code>null</code> for tokens that do not have to
2138 be styled. For your styles, you are encouraged to use the
2139 'standard' names defined in the themes (without
2140 the <code>cm-</code> prefix). If that fails, it is also possible
2141 to come up with your own and write your own CSS theme file.<p>
2143 <p id="StringStream">The stream object that's passed
2144 to <code>token</code> encapsulates a line of code (tokens may
2145 never span lines) and our current position in that line. It has
2146 the following API:</p>
2149 <dt><code><strong></strong><strong>eol</strong>() → boolean</code></dt>
2150 <dd>Returns true only if the stream is at the end of the
2152 <dt><code><strong></strong><strong>sol</strong>() → boolean</code></dt>
2153 <dd>Returns true only if the stream is at the start of the
2156 <dt><code><strong>peek</strong>() → string</code></dt>
2157 <dd>Returns the next character in the stream without advancing
2158 it. Will return an <code>null</code> at the end of the
2160 <dt><code><strong>next</strong>() → string</code></dt>
2161 <dd>Returns the next character in the stream and advances it.
2162 Also returns <code>null</code> when no more characters are
2165 <dt><code><strong>eat</strong>(match: string|regexp|function(char: string) → boolean) → string</code></dt>
2166 <dd><code>match</code> can be a character, a regular expression,
2167 or a function that takes a character and returns a boolean. If
2168 the next character in the stream 'matches' the given argument,
2169 it is consumed and returned. Otherwise, <code>undefined</code>
2171 <dt><code><strong>eatWhile</strong>(match: string|regexp|function(char: string) → boolean) → boolean</code></dt>
2172 <dd>Repeatedly calls <code>eat</code> with the given argument,
2173 until it fails. Returns true if any characters were eaten.</dd>
2174 <dt><code><strong>eatSpace</strong>() → boolean</code></dt>
2175 <dd>Shortcut for <code>eatWhile</code> when matching
2177 <dt><code><strong>skipToEnd</strong>()</code></dt>
2178 <dd>Moves the position to the end of the line.</dd>
2179 <dt><code><strong>skipTo</strong>(ch: string) → boolean</code></dt>
2180 <dd>Skips to the next occurrence of the given character, if
2181 found on the current line (doesn't advance the stream if the
2182 character does not occur on the line). Returns true if the
2183 character was found.</dd>
2184 <dt><code><strong>match</strong>(pattern: string, ?consume: boolean, ?caseFold: boolean) → boolean</code></dt>
2185 <dt><code><strong>match</strong>(pattern: regexp, ?consume: boolean) → array<string></code></dt>
2187 multi-character <code>eat</code>—if <code>consume</code> is true
2188 or not given—or a look-ahead that doesn't update the stream
2189 position—if it is false. <code>pattern</code> can be either a
2190 string or a regular expression starting with <code>^</code>.
2191 When it is a string, <code>caseFold</code> can be set to true to
2192 make the match case-insensitive. When successfully matching a
2193 regular expression, the returned value will be the array
2194 returned by <code>match</code>, in case you need to extract
2195 matched groups.</dd>
2197 <dt><code><strong>backUp</strong>(n: integer)</code></dt>
2198 <dd>Backs up the stream <code>n</code> characters. Backing it up
2199 further than the start of the current token will cause things to
2200 break, so be careful.</dd>
2201 <dt><code><strong>column</strong>() → integer</code></dt>
2202 <dd>Returns the column (taking into account tabs) at which the
2203 current token starts.</dd>
2204 <dt><code><strong>indentation</strong>() → integer</code></dt>
2205 <dd>Tells you how far the current line has been indented, in
2206 spaces. Corrects for tab characters.</dd>
2208 <dt><code><strong>current</strong>() → string</code></dt>
2209 <dd>Get the string between the start of the current token and
2210 the current stream position.</dd>
2213 <p id="blankLine">By default, blank lines are simply skipped when
2214 tokenizing a document. For languages that have significant blank
2215 lines, you can define a <code>blankLine(state)</code> method on
2216 your mode that will get called whenever a blank line is passed
2217 over, so that it can update the parser state.</p>
2219 <p id="copyState">Because state object are mutated, and CodeMirror
2220 needs to keep valid versions of a state around so that it can
2221 restart a parse at any line, copies must be made of state objects.
2222 The default algorithm used is that a new state object is created,
2223 which gets all the properties of the old object. Any properties
2224 which hold arrays get a copy of these arrays (since arrays tend to
2225 be used as mutable stacks). When this is not correct, for example
2226 because a mode mutates non-array properties of its state object, a
2227 mode object should define a <code>copyState</code> method,
2228 which is given a state and should return a safe copy of that
2231 <p id="indent">If you want your mode to provide smart indentation
2232 (through the <a href="#indentLine"><code>indentLine</code></a>
2233 method and the <code>indentAuto</code>
2234 and <code>newlineAndIndent</code> commands, to which keys can be
2235 <a href="#option_extraKeys">bound</a>), you must define
2236 an <code>indent(state, textAfter)</code> method on your mode
2239 <p>The indentation method should inspect the given state object,
2240 and optionally the <code>textAfter</code> string, which contains
2241 the text on the line that is being indented, and return an
2242 integer, the amount of spaces to indent. It should usually take
2243 the <a href="#option_indentUnit"><code>indentUnit</code></a>
2244 option into account. An indentation method may
2245 return <code>CodeMirror.Pass</code> to indicate that it
2246 could not come up with a precise indentation.</p>
2248 <p id="mode_comment">To work well with
2249 the <a href="#addon_comment">commenting addon</a>, a mode may
2250 define <code>lineComment</code> (string that starts a line
2251 comment), <code>blockCommentStart</code>, <code>blockCommentEnd</code>
2252 (strings that start and end block comments),
2253 and <code>blockCommentLead</code> (a string to put at the start of
2254 continued lines in a block comment). All of these are
2257 <p id="electricChars">Finally, a mode may define
2258 an <code>electricChars</code> property, which should hold a string
2259 containing all the characters that should trigger the behaviour
2261 the <a href="#option_electricChars"><code>electricChars</code></a>
2264 <p>So, to summarize, a mode <em>must</em> provide
2265 a <code>token</code> method, and it <em>may</em>
2266 provide <code>startState</code>, <code>copyState</code>,
2267 and <code>indent</code> methods. For an example of a trivial mode,
2268 see the <a href="../mode/diff/diff.js">diff mode</a>, for a more
2269 involved example, see the <a href="../mode/clike/clike.js">C-like
2272 <p>Sometimes, it is useful for modes to <em>nest</em>—to have one
2273 mode delegate work to another mode. An example of this kind of
2274 mode is the <a href="../mode/htmlmixed/htmlmixed.js">mixed-mode HTML
2275 mode</a>. To implement such nesting, it is usually necessary to
2276 create mode objects and copy states yourself. To create a mode
2277 object, there are <code>CodeMirror.getMode(options,
2278 parserConfig)</code>, where the first argument is a configuration
2279 object as passed to the mode constructor function, and the second
2280 argument is a mode specification as in
2281 the <a href="#option_mode"><code>mode</code></a> option. To copy a
2282 state object, call <code>CodeMirror.copyState(mode, state)</code>,
2283 where <code>mode</code> is the mode that created the given
2286 <p id="innerMode">In a nested mode, it is recommended to add an
2287 extra methods, <code>innerMode</code> which, given a state object,
2288 returns a <code>{state, mode}</code> object with the inner mode
2289 and its state for the current position. These are used by utility
2290 scripts such as the <a href="#addon_closetag">tag closer</a> to
2291 get context information. Use the <code>CodeMirror.innerMode</code>
2292 helper function to, starting from a mode and a state, recursively
2293 walk down to the innermost mode and state.</p>
2295 <p>To make indentation work properly in a nested parser, it is
2296 advisable to give the <code>startState</code> method of modes that
2297 are intended to be nested an optional argument that provides the
2298 base indentation for the block of code. The JavaScript and CSS
2299 parser do this, for example, to allow JavaScript and CSS code
2300 inside the mixed-mode HTML mode to be properly indented.</p>
2302 <p>It is possible, and encouraged, to associate your mode, or a
2303 certain configuration of your mode, with
2304 a <a href="http://en.wikipedia.org/wiki/MIME">MIME</a> type. For
2305 example, the JavaScript mode associates itself
2306 with <code>text/javascript</code>, and its JSON variant
2307 with <code>application/json</code>. To do this,
2308 call <code>CodeMirror.defineMIME(mime, modeSpec)</code>,
2309 where <code>modeSpec</code> can be a string or object specifying a
2310 mode, as in the <a href="#option_mode"><code>mode</code></a>
2313 <p id="extendMode">Sometimes, it is useful to add or override mode
2314 object properties from external code.
2315 The <code>CodeMirror.extendMode</code> can be used to add
2316 properties to mode objects produced for a specific mode. Its first
2317 argument is the name of the mode, its second an object that
2318 specifies the properties that should be added. This is mostly
2319 useful to add utilities that can later be looked
2320 up through <a href="#getMode"><code>getMode</code></a>.</p>
2322 </div><div class="rightsmall blk">
2327 <li><a href="#overview">Overview</a></li>
2328 <li><a href="#usage">Basic Usage</a></li>
2329 <li><a href="#config">Configuration</a></li>
2330 <li><a href="#events">Events</a></li>
2331 <li><a href="#keymaps">Keymaps</a></li>
2332 <li><a href="#styling">Customized Styling</a></li>
2333 <li><a href="#api">Programming API</a>
2335 <li><a href="#constructor">Constructor</a></li>
2336 <li><a href="#api_content">Content manipulation</a></li>
2337 <li><a href="#api_selection">Selection</a></li>
2338 <li><a href="#api_configuration">Configuration</a></li>
2339 <li><a href="#api_doc">Document management</a></li>
2340 <li><a href="#api_history">History</a></li>
2341 <li><a href="#api_marker">Text-marking</a></li>
2342 <li><a href="#api_decoration">Widget, gutter, and decoration</a></li>
2343 <li><a href="#api_sizing">Sizing, scrolling, and positioning</a></li>
2344 <li><a href="#api_mode">Mode, state, and tokens</a></li>
2345 <li><a href="#api_misc">Miscellaneous methods</a></li>
2346 <li><a href="#api_static">Static properties</a></li>
2349 <li><a href="#addons">Addons</a></li>
2350 <li><a href="#modeapi">Writing CodeMirror Modes</a></li>
2355 <div style="height: 2em"> </div>
2357 <script>setTimeout(function(){CodeMirror.colorize();}, 20);</script>