1
0

manual.html 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>CodeMirror: User Manual</title>
  5. <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Droid+Sans|Droid+Sans:bold"/>
  6. <link rel="stylesheet" type="text/css" href="css/docs.css"/>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  8. <style>dl dl {margin: 0;}</style>
  9. </head>
  10. <body>
  11. <h1><span class="logo-braces">{ }</span> <a href="http://codemirror.net/">CodeMirror</a></h1>
  12. <pre class="grey">
  13. <img src="css/baboon.png" class="logo" alt="logo"/>/* User manual and
  14. reference guide */
  15. </pre>
  16. <div class="clear"><div class="leftbig blk">
  17. <h2 id="overview">Overview</h2>
  18. <p>CodeMirror is a code-editor component that can be embedded in
  19. Web pages. It provides <em>only</em> the editor component, no
  20. accompanying buttons
  21. (see <a href="http://www.octolabs.com/javascripts/codemirror-ui/">CodeMirror
  22. UI</a> for a drop-in button bar), auto-completion, or other IDE
  23. functionality. It does provide a rich API on top of which such
  24. functionality can be straightforwardly implemented.</p>
  25. <p>CodeMirror works with language-specific modes. Modes are
  26. JavaScript programs that help color (and optionally indent) text
  27. written in a given language. The distribution comes with a few
  28. modes (see the <code>mode/</code> directory), and it isn't hard
  29. to <a href="#modeapi">write new ones</a> for other languages.</p>
  30. <h2 id="usage">Basic Usage</h2>
  31. <p>The easiest way to use CodeMirror is to simply load the script
  32. and style sheet found under <code>lib/</code> in the distribution,
  33. plus a mode script from one of the <code>mode/</code> directories
  34. and a theme stylesheet from <code>theme/</code>. (See
  35. also <a href="compress.html">the compresion helper</a>.) For
  36. example:</p>
  37. <pre>&lt;script src="lib/codemirror.js">&lt;/script>
  38. &lt;link rel="stylesheet" href="lib/codemirror.css">
  39. &lt;script src="mode/javascript/javascript.js">&lt;/script>
  40. &lt;link rel="stylesheet" href="theme/default.css"></pre>
  41. <p>(If you use a them other than <code>default.css</code>, you
  42. also have to specify the
  43. <a href="#option_theme"><code>theme</code></a> option.) Having
  44. done this, an editor instance can be created like this:</p>
  45. <pre>var myCodeMirror = CodeMirror(document.body);</pre>
  46. <p>The editor will be appended to the document body, will start
  47. empty, and will use the mode that we loaded. To have more control
  48. over the new editor, a configuration object can be passed
  49. to <code>CodeMirror</code> as a second argument:</p>
  50. <pre>var myCodeMirror = CodeMirror(document.body, {
  51. value: "function myScript(){return 100;}\n",
  52. mode: "javascript"
  53. });</pre>
  54. <p>This will initialize the editor with a piece of code already in
  55. it, and explicitly tell it to use the JavaScript mode (which is
  56. useful when multiple modes are loaded).
  57. See <a href="#config">below</a> for a full discussion of the
  58. configuration options that CodeMirror accepts.</p>
  59. <p>In cases where you don't want to append the editor to an
  60. element, and need more control over the way it is inserted, the
  61. first argument to the <code>CodeMirror</code> function can also
  62. be a function that, when given a DOM element, inserts it into the
  63. document somewhere. This could be used to, for example, replace a
  64. textarea with a real editor:</p>
  65. <pre>var myCodeMirror = CodeMirror(function(elt) {
  66. myTextArea.parentNode.replaceChild(elt, myTextArea);
  67. }, {value: myTextArea.value});</pre>
  68. <p>However, for this use case, which is a common way to use
  69. CodeMirror, the library provides a much more powerful
  70. shortcut:</p>
  71. <pre>var myCodeMirror = CodeMirror.fromTextArea(myTextArea);</pre>
  72. <p>This will, among other things, ensure that the textarea's value
  73. is updated when the form (if it is part of a form) is submitted.
  74. See the <a href="#fromTextArea">API reference</a> for a full
  75. description of this method.</p>
  76. <h2 id="config">Configuration</h2>
  77. <p>Both the <code>CodeMirror</code> function and
  78. its <code>fromTextArea</code> method take as second (optional)
  79. argument an object containing configuration options. Any option
  80. not supplied like this will be taken
  81. from <code>CodeMirror.defaults</code>, an object containing the
  82. default options. You can update this object to change the defaults
  83. on your page.</p>
  84. <p>Options are not checked in any way, so setting bogus option
  85. values is bound to lead to odd errors.</p>
  86. <p><em>Note:</em> CodeMirror
  87. 2 <a href="internals.html#summary">does not support</a>
  88. line-wrapping. I would have very much liked to support it, but it
  89. combines extremely poorly with the way the editor is
  90. implemented.</p>
  91. <p>These are the supported options:</p>
  92. <dl>
  93. <dt id="option_value"><code>value (string)</code></dt>
  94. <dd>The starting value of the editor.</dd>
  95. <dt id="option_mode"><code>mode (string or object)</code></dt>
  96. <dd>The mode to use. When not given, this will default to the
  97. first mode that was loaded. It may be a string, which either
  98. simply names the mode or is
  99. a <a href="http://en.wikipedia.org/wiki/MIME">MIME</a> type
  100. associated with the mode. Alternatively, it may be an object
  101. containing configuration options for the mode, with
  102. a <code>name</code> property that names the mode (for
  103. example <code>{name: "javascript", json: true}</code>). The demo
  104. pages for each mode contain information about what configuration
  105. parameters the mode supports. You can ask CodeMirror which modes
  106. and MIME types are loaded with
  107. the <code>CodeMirror.listModes</code>
  108. and <code>CodeMirror.listMIMEs</code> functions.</dd>
  109. <dt id="option_theme"><code>theme (string)</code></dt>
  110. <dd>The theme to style the editor with. You must make sure the
  111. CSS file defining the corresponding <code>.cm-s-[name]</code>
  112. styles is loaded (see
  113. the <a href="theme/"><code>theme</code></a> directory in the
  114. distribution).</dd>
  115. <dt id="option_indentUnit"><code>indentUnit (integer)</code></dt>
  116. <dd>How many spaces a block (whatever that means in the edited
  117. language) should be indented. The default is 2.</dd>
  118. <dt id="option_indentWithTabs"><code>indentWithTabs (boolean)</code></dt>
  119. <dd>Whether, when indenting, the first N*8 spaces should be
  120. replaced by N tabs. Default is false.</dd>
  121. <dt id="option_tabMode"><code>tabMode (string)</code></dt>
  122. <dd>Determines what happens when the user presses the tab key.
  123. Must be one of the following:
  124. <dl>
  125. <dt><code>"classic" (the default)</code></dt>
  126. <dd>When nothing is selected, insert a tab. Otherwise,
  127. behave like the <code>"shift"</code> mode. (When shift is
  128. held, this behaves like the <code>"indent"</code> mode.)</dd>
  129. <dt><code>"shift"</code></dt>
  130. <dd>Indent all selected lines by
  131. one <a href="#option_indentUnit"><code>indentUnit</code></a>.
  132. If shift was held while pressing tab, un-indent all selected
  133. lines one unit.</dd>
  134. <dt><code>"indent"</code></dt>
  135. <dd>Indent the line the 'correctly', based on its syntactic
  136. context. Only works if the
  137. mode <a href="#indent">supports</a> it.</dd>
  138. <dt><code>"default"</code></dt>
  139. <dd>Do not capture tab presses, let the browser apply its
  140. default behaviour (which usually means it skips to the next
  141. control).</dd>
  142. </dl></dd>
  143. <dt id="option_enterMode"><code>enterMode (string)</code></dt>
  144. <dd>Determines whether and how new lines are indented when the
  145. enter key is pressed. The following modes are supported:
  146. <dl>
  147. <dt><code>"indent" (the default)</code></dt>
  148. <dd>Use the mode's indentation rules to give the new line
  149. the correct indentation.</dd>
  150. <dt><code>"keep"</code></dt>
  151. <dd>Indent the line the same as the previous line.</dd>
  152. <dt><code>"flat"</code></dt>
  153. <dd>Do not indent the new line.</dd>
  154. </dl></dd>
  155. <dt id="option_electricChars"><code>electricChars (boolean)</code></dt>
  156. <dd>Configures whether the editor should re-indent the current
  157. line when a character is typed that might change its proper
  158. indentation (only works if the mode supports indentation).
  159. Default is true.</dd>
  160. <dt id="option_lineNumbers"><code>lineNumbers (boolean)</code></dt>
  161. <dd>Whether to show line numbers to the left of the editor.</dd>
  162. <dt id="option_firstLineNumber"><code>firstLineNumber (integer)</code></dt>
  163. <dd>At which number to start counting lines. Default is 1.</dd>
  164. <dt id="option_gutter"><code>gutter (boolean)</code></dt>
  165. <dd>Can be used to force a 'gutter' (empty space on the left of
  166. the editor) to be shown even when no line numbers are active.
  167. This is useful for setting <a href="#setMarker">markers</a>.</dd>
  168. <dt id="option_readOnly"><code>readOnly (boolean)</code></dt>
  169. <dd>This disables editing of the editor content by the user.
  170. (Changes through API functions will still be possible.) If you
  171. also want to disable the cursor, use <code>"nocursor"</code> as
  172. a value for this option, instead of <code>true</code>.</dd>
  173. <dt id="option_onChange"><code>onChange (function)</code></dt>
  174. <dd>When given, this function will be called every time the
  175. content of the editor is changed. It will be given the editor
  176. instance as only argument.</dd>
  177. <dt id="option_onCursorActivity"><code>onCursorActivity (function)</code></dt>
  178. <dd>Like <code>onChange</code>, but will also be called when the
  179. cursor moves without any changes being made.</dd>
  180. <dt id="option_onGutterClick"><code>onGutterClick (function)</code></dt>
  181. <dd>When given, will be called whenever the editor gutter (the
  182. line-number area) is clicked. Will be given the editor instance
  183. as first argument, and the (zero-based) number of the line that
  184. was clicked as second argument.</dd>
  185. <dt id="option_onFocus"><code>onFocus, onBlur (function)</code></dt>
  186. <dd>The given functions will be called whenever the editor is
  187. focused or unfocused.</dd>
  188. <dt id="option_onScroll"><code>onScroll (function)</code></dt>
  189. <dd>When given, will be called whenever the editor is
  190. scrolled.</dd>
  191. <dt id="option_onHighlightComplete"><code>onHighlightComplete (function)</code></dt>
  192. <dd>Whenever the editor's content has been fully highlighted,
  193. this function (if given) will be called. It'll be given a single
  194. argument, the editor instance.</dd>
  195. <dt id="option_matchBrackets"><code>matchBrackets (boolean)</code></dt>
  196. <dd>Determines whether brackets are matched whenever the cursor
  197. is moved next to a bracket.</dd>
  198. <dt id="option_workTime"><code>workTime, workDelay (number)</code></dt>
  199. <dd>Highlighting is done by a pseudo background-thread that will
  200. work for <code>workTime</code> milliseconds, and then use
  201. timeout to sleep for <code>workDelay</code> milliseconds. The
  202. defaults are 200 and 300, you can change these options to make
  203. the highlighting more or less aggressive.</dd>
  204. <dt id="option_undoDepth"><code>undoDepth (integer)</code></dt>
  205. <dd>The maximum number of undo levels that the editor stores.
  206. Defaults to 40.</dd>
  207. <dt id="option_tabindex"><code>tabindex (integer)</code></dt>
  208. <dd>The <a href="http://www.w3.org/TR/html401/interact/forms.html#adef-tabindex">tab
  209. index</a> to assign to the editor. If not given, no tab index
  210. will be assigned.</dd>
  211. <dt id="option_document"><code>document (DOM document)</code></dt>
  212. <dd>Use this if you want to display the editor in another DOM.
  213. By default it will use the global <code>document</code>
  214. object.</dd>
  215. <dt id="option_onKeyEvent"><code>onKeyEvent (function)</code></dt>
  216. <dd>This provides a rather low-level hook into CodeMirror's key
  217. handling. If provided, this function will be called on
  218. every <code>keydown</code>, <code>keyup</code>,
  219. and <code>keypress</code> event that CodeMirror captures. It
  220. will be passed two arguments, the editor instance and the key
  221. event. This key event is pretty much the raw key event, except
  222. that a <code>stop()</code> method is always added to it. You
  223. could feed it to, for example, <code>jQuery.Event</code> to
  224. further normalize it.<br>This function can inspect the key
  225. event, and handle it if it wants to. It may return true to tell
  226. CodeMirror to ignore the event. Be wary that, on some browsers,
  227. stopping a <code>keydown</code> does not stop
  228. the <code>keypress</code> from firing, whereas on others it
  229. does. If you respond to an event, you should probably inspect
  230. its <code>type</code> property and only do something when it
  231. is <code>keydown</code> (or <code>keypress</code> for actions
  232. that need character data).</dd>
  233. </dl>
  234. <h2 id="styling">Customized Styling</h2>
  235. <p>Up to a certain extent, CodeMirror's look can be changed by
  236. modifying style sheet files. The style sheets supplied by modes
  237. simply provide the colors for that mode, and can be adapted in a
  238. very straightforward way. To style the editor itself, it is
  239. possible to alter or override the styles defined
  240. in <a href="lib/codemirror.css"><code>codemirror.css</code></a>.</p>
  241. <p>Some care must be taken there, since a lot of the rules in this
  242. file are necessary to have CodeMirror function properly. Adjusting
  243. colors should be safe, of course, and with some care a lot of
  244. other things can be changed as well. The CSS classes defined in
  245. this file serve the following roles:</p>
  246. <dl>
  247. <dt id="class_CodeMirror"><code>CodeMirror</code></dt>
  248. <dd>The outer element of the editor. This should be used for
  249. borders and positioning. Can also be used to set styles that
  250. should hold for everything inside the editor (such as font
  251. and font size), or to set a background.</dd>
  252. <dt id="class_CodeMirror_scroll"><code>CodeMirror-scroll</code></dt>
  253. <dd>This determines whether the editor scrolls (<code>overflow:
  254. auto</code> + fixed height). By default, it does. Giving
  255. this <code>height: auto; overflow: visible;</code> will cause
  256. the editor to resize to fit its content.</dd>
  257. <dt id="class_CodeMirror_focused"><code>CodeMirror-focused</code></dt>
  258. <dd>Whenever the editor is focused, the top element gets this
  259. class. This is used to hide the cursor and give the selection a
  260. different color when the editor is not focused.</dd>
  261. <dt id="class_CodeMirror_gutter"><code>CodeMirror-gutter</code></dt>
  262. <dd>Use this for giving a background or a border to the editor
  263. gutter. Don't set any padding here,
  264. use <code>CodeMirror-gutter-text</code> for that. By default,
  265. the gutter is 'fluid', meaning it will adjust its width to the
  266. maximum line number or line marker width. You can also set a
  267. fixed width if you want.</dd>
  268. <dt id="class_CodeMirror_gutter_text"><code>CodeMirror-gutter-text</code></dt>
  269. <dd>Used to style the actual line numbers. For the numbers to
  270. line up, you must make sure that the font in the gutter is the
  271. same as the one in the rest of the editor, so you should
  272. probably only set font style and size in
  273. the <code>CodeMirror</code> class.</dd>
  274. <dt id="class_CodeMirror_lines"><code>CodeMirror-lines</code></dt>
  275. <dd>The visible lines. If this has vertical
  276. padding, <code>CodeMirror-gutter</code> should have the same
  277. padding.</dd>
  278. <dt id="class_CodeMirror_cursor"><code>CodeMirror-cursor</code></dt>
  279. <dd>The cursor is a block element that is absolutely positioned.
  280. You can make it look whichever way you want.</dd>
  281. <dt id="class_CodeMirror_selected"><code>CodeMirror-selected</code></dt>
  282. <dd>The selection is represented by <code>span</code> elements
  283. with this class.</dd>
  284. <dt id="class_CodeMirror_matchingbracket"><code>CodeMirror-matchingbracket</code>,
  285. <code>CodeMirror-nonmatchingbracket</code></dt>
  286. <dd>These are used to style matched (or unmatched) brackets.</dd>
  287. </dl>
  288. <p>The actual lines, as well as the cursor, are represented
  289. by <code>pre</code> elements. By default no text styling (such as
  290. bold) that might change line height is applied. If you do want
  291. such effects, you'll have to give <code>CodeMirror pre</code> a
  292. fixed height. Also, you must still take care that character width
  293. is constant.</p>
  294. <p>If your page's style sheets do funky things to
  295. all <code>div</code> or <code>pre</code> elements (you probably
  296. shouldn't do that), you'll have to define rules to cancel these
  297. effects out again for elements under the <code>CodeMirror</code>
  298. class.</p>
  299. <h2 id="api">Programming API</h2>
  300. <p>A lot of CodeMirror features are only available through its API.
  301. This has the disadvantage that you need to do work to enable them,
  302. and the advantage that CodeMirror will fit seamlessly into your
  303. application.</p>
  304. <p>Whenever points in the document are represented, the API uses
  305. objects with <code>line</code> and <code>ch</code> properties.
  306. Both are zero-based. CodeMirror makes sure to 'clip' any positions
  307. passed by client code so that they fit inside the document, so you
  308. shouldn't worry too much about sanitizing your coordinates. If you
  309. give <code>ch</code> a value of <code>null</code>, or don't
  310. specify it, it will be replaced with the length of the specified
  311. line.</p>
  312. <dl>
  313. <dt id="getValue"><code>getValue() → string</code></dt>
  314. <dd>Get the current editor content.</dd>
  315. <dt id="setValue"><code>setValue(string)</code></dt>
  316. <dd>Set the editor content.</dd>
  317. <dt id="getSelection"><code>getSelection() → string</code></dt>
  318. <dd>Get the currently selected code.</dd>
  319. <dt id="replaceSelection"><code>replaceSelection(string)</code></dt>
  320. <dd>Replace the selection with the given string.</dd>
  321. <dt id="focus"><code>focus()</code></dt>
  322. <dd>Give the editor focus.</dd>
  323. <dt id="setOption"><code>setOption(option, value)</code></dt>
  324. <dd>Change the configuration of the editor. <code>option</code>
  325. should the name of an <a href="#config">option</a>,
  326. and <code>value</code> should be a valid value for that
  327. option.</dd>
  328. <dt id="getOption"><code>getOption(option) → value</code></dt>
  329. <dd>Retrieves the current value of the given option for this
  330. editor instance.</dd>
  331. <dt id="cursorCoords"><code>cursorCoords(start) → object</code></dt>
  332. <dd>Returns an <code>{x, y, yBot}</code> object containing the
  333. coordinates of the cursor relative to the top-left corner of the
  334. page. <code>yBot</code> is the coordinate of the bottom of the
  335. cursor. <code>start</code> is a boolean indicating whether you
  336. want the start or the end of the selection.</dd>
  337. <dt id="charCoords"><code>charCoords(pos) → object</code></dt>
  338. <dd>Like <code>cursorCoords</code>, but returns the position of
  339. an arbitrary characters. <code>pos</code> should be
  340. a <code>{line, ch}</code> object.</dd>
  341. <dt id="coordsChar"><code>coordsChar(object) → pos</code></dt>
  342. <dd>Given an <code>{x, y}</code> object (in page coordinates),
  343. returns the <code>{line, ch}</code> position that corresponds to
  344. it.</dd>
  345. <dt id="undo"><code>undo()</code></dt>
  346. <dd>Undo one edit (if any undo events are stored).</dd>
  347. <dt id="redo"><code>redo()</code></dt>
  348. <dd>Redo one undone edit.</dd>
  349. <dt id="historySize"><code>historySize() → object</code></dt>
  350. <dd>Returns an object with <code>{undo, redo}</code> properties,
  351. both of which hold integers, indicating the amount of stored
  352. undo and redo operations.</dd>
  353. <dt id="indentLine"><code>indentLine(line)</code></dt>
  354. <dd>Reset the given line's indentation to the indentation
  355. prescribed by the mode.</dd>
  356. <dt id="getSearchCursor"><code>getSearchCursor(query, start, caseFold) → cursor</code></dt>
  357. <dd>Used to implement search/replace
  358. functionality. <code>query</code> can be a regular expression or
  359. a string (only strings will match across lines—if they contain
  360. newlines). <code>start</code> provides the starting position of
  361. the search. It can be a <code>{line, ch}</code> object, or can
  362. be left off to default to the start of the
  363. document. <code>caseFold</code> is only relevant when matching a
  364. string. It will cause the search to be case-insensitive. A
  365. search cursor has the following methods:
  366. <dl>
  367. <dt><code>findNext(), findPrevious() → boolean</code></dt>
  368. <dd>Search forward or backward from the current position.
  369. The return value indicates whether a match was found. If
  370. matching a regular expression, the return value will be the
  371. array returned by the <code>match</code> method, in case you
  372. want to extract matched groups.</dd>
  373. <dt><code>from(), to() → object</code></dt>
  374. <dd>These are only valid when the last call
  375. to <code>findNext</code> or <code>findPrevious</code> did
  376. not return false. They will return <code>{line, ch}</code>
  377. objects pointing at the start and end of the match.</dd>
  378. <dt><code>replace(text)</code></dt>
  379. <dd>Replaces the currently found match with the given text
  380. and adjusts the cursor position to reflect the
  381. replacement.</dd>
  382. </dl></dd>
  383. <dt id="getTokenAt"><code>getTokenAt(pos) → object</code></dt>
  384. <dd>Retrieves information about the token the current mode found
  385. at the given position (a <code>{line, ch}</code> object). The
  386. returned object has the following properties:
  387. <dl>
  388. <dt><code>start</code></dt><dd>The character (on the given line) at which the token starts.</dd>
  389. <dt><code>end</code></dt><dd>The character at which the token ends.</dd>
  390. <dt><code>string</code></dt><dd>The token's string.</dd>
  391. <dt><code>className</code></dt><dd>The class the mode assigned
  392. to the token. (Can be null when no class was assigned.)</dd>
  393. <dt><code>state</code></dt><dd>The mode's state at the end of this token.</dd>
  394. </dl></dd>
  395. <dt id="markText"><code>markText(from, to, className) → function</code></dt>
  396. <dd>Can be used to mark a range of text with a specific CSS
  397. class name. <code>from</code> and <code>to</code> should
  398. be <code>{line, ch}</code> objects. The method will return a
  399. function that can be called to remove the marking.</dd>
  400. <dt id="setMarker"><code>setMarker(line, text, className) → lineHandle</code></dt>
  401. <dd>Add a gutter marker for the given line. Gutter markers are
  402. shown in the line-number area (instead of the number for this
  403. line). Both <code>text</code> and <code>className</code> are
  404. optional. Setting <code>text</code> to a Unicode character like
  405. ● tends to give a nice effect. To put a picture in the gutter,
  406. set <code>text</code> to a space and <code>className</code> to
  407. something that sets a background image. If you
  408. specify <code>text</code>, the given text (which may contain
  409. HTML) will, by default, replace the line number for that line.
  410. If this is not what you want, you can include the
  411. string <code>%N%</code> in the text, which will be replaced by
  412. the line number.</dd>
  413. <dt id="clearMarker"><code>clearMarker(line)</code></dt>
  414. <dd>Clears a marker created
  415. with <code>setMarker</code>. <code>line</code> can be either a
  416. number or a handle returned by <code>setMarker</code> (since a
  417. number may now refer to a different line if something was added
  418. or deleted).</dd>
  419. <dt id="setLineClass"><code>setLineClass(line, className) → lineHandle</code></dt>
  420. <dd>Set a CSS class name for the given line. <code>line</code>
  421. can be a number or a line handle (as returned
  422. by <code>setMarker</code> or this function).
  423. Pass <code>null</code> to clear the class for a line.</dd>
  424. <dt id="lineInfo"><code>lineInfo(line) → object</code></dt>
  425. <dd>Returns the line number, text content, and marker status of
  426. the given line, which can be either a number or a handle
  427. returned by <code>setMarker</code>. The returned object has the
  428. structure <code>{line, text, markerText, markerClass}</code>.</dd>
  429. <dt id="addWidget"><code>addWidget(pos, node, scrollIntoView)</code></dt>
  430. <dd>Puts <code>node</code>, which should be an absolutely
  431. positioned DOM node, into the editor, positioned right below the
  432. given <code>{line, ch}</code> position.
  433. When <code>scrollIntoView</code> is true, the editor will ensure
  434. that the entire node is visible (if possible). To remove the
  435. widget again, simply use DOM methods (move it somewhere else, or
  436. call <code>removeChild</code> on its parent).</dd>
  437. <dt id="matchBrackets"><code>matchBrackets()</code></dt>
  438. <dd>Force matching-bracket-highlighting to happen.</dd>
  439. <dt id="lineCount"><code>lineCount() → number</code></dt>
  440. <dd>Get the number of lines in the editor.</dd>
  441. <dt id="getCursor"><code>getCursor(start) → object</code></dt>
  442. <dd><code>start</code> is a boolean indicating whether the start
  443. or the end of the selection must be retrieved. If it is not
  444. given, the current cursor pos, i.e. the side of the selection
  445. that would move if you pressed an arrow key, is chosen.
  446. A <code>{line, ch}</code> object will be returned.</dd>
  447. <dt id="somethingSelected"><code>somethingSelected() → boolean</code></dt>
  448. <dd>Return true if any text is selected.</dd>
  449. <dt id="setCursor"><code>setCursor(pos)</code></dt>
  450. <dd>Set the cursor position. You can either pass a
  451. single <code>{line, ch}</code> object, or the line and the
  452. character as two separate parameters.</dd>
  453. <dt id="setSelection"><code>setSelection(start, end)</code></dt>
  454. <dd>Set the selection range. <code>start</code>
  455. and <code>end</code> should be <code>{line, ch}</code> objects.</dd>
  456. <dt id="getLine"><code>getLine(n) → string</code></dt>
  457. <dd>Get the content of line <code>n</code>.</dd>
  458. <dt id="setLine"><code>setLine(n, text)</code></dt>
  459. <dd>Set the content of line <code>n</code>.</dd>
  460. <dt id="removeLine"><code>removeLine(n)</code></dt>
  461. <dd>Remove the given line from the document.</dd>
  462. <dt id="getRange"><code>getRange(from, to) → string</code></td>
  463. <dd>Get the text between the given points in the editor, which
  464. should be <code>{line, ch}</code> objects.</dd>
  465. <dt id="replaceRange"><code>replaceRange(string, from, to)</code></dt>
  466. <dd>Replace the part of the document between <code>from</code>
  467. and <code>to</code> with the given string. <code>from</code>
  468. and <code>to</code> must be <code>{line, ch}</code>
  469. objects. <code>to</code> can be left off to simply insert the
  470. string at position <code>from</code>.</dd>
  471. </dl>
  472. <p>The following are more low-level methods:</p>
  473. <dl>
  474. <dt id="operation"><code>operation(func) → result</code></dt>
  475. <dd>CodeMirror internally buffers changes and only updates its
  476. DOM structure after it has finished performing some operation.
  477. If you need to perform a lot of operations on a CodeMirror
  478. instance, you can call this method with a function argument. It
  479. will call the function, buffering up all changes, and only doing
  480. the expensive update after the function returns. This can be a
  481. lot faster. The return value from this method will be the return
  482. value of your function.</dd>
  483. <dt id="refresh"><code>refresh()</code></dt>
  484. <dd>If your code does something to change the size of the editor
  485. element (window resizes are already listened for), or unhides
  486. it, you should probably follow up by calling this method to
  487. ensure CodeMirror is still looking as intended.</dd>
  488. <dt id="getInputField"><code>getInputField() → textarea</code></dt>
  489. <dd>Returns the hiden textarea used to read input.</dd>
  490. <dt id="getWrapperElement"><code>getWrapperElement() → node</code></dt>
  491. <dd>Returns the DOM node that represents the editor. Remove this
  492. from your tree to delete an editor instance.</dd>
  493. <dt id="getScrollerElement"><code>getScrollerElement() → node</code></dt>
  494. <dd>Returns the DOM node that is responsible for the sizing and
  495. the scrolling of the editor. You can change
  496. the <code>height</code> and <code>width</code> styles of this
  497. element to resize an editor. (You might have to call
  498. the <a href="#refresh"><code>refresh</code></a> method
  499. afterwards.)</dd>
  500. <dt id="getStateAfter"><code>getStateAfter(line) → state</code></dt>
  501. <dd>Returns the mode's parser state, if any, at the end of the
  502. given line number. If no line number is given, the state at the
  503. end of the document is returned. This can be useful for storing
  504. parsing errors in the state, or getting other kinds of
  505. contextual information for a line.</dd>
  506. </dl>
  507. <p id="fromTextArea">Finally, the <code>CodeMirror</code> object
  508. itself has a method <code>fromTextArea</code>. This takes a
  509. textarea DOM node as first argument and an optional configuration
  510. object as second. It will replace the textarea with a CodeMirror
  511. instance, and wire up the form of that textarea (if any) to make
  512. sure the editor contents are put into the textarea when the form
  513. is submitted. A CodeMirror instance created this way has two
  514. additional methods:</p>
  515. <dl>
  516. <dt id="save"><code>save()</code></dt>
  517. <dd>Copy the content of the editor into the textarea.</dd>
  518. <dt id="toTextArea"><code>toTextArea()</code></dt>
  519. <dd>Remove the editor, and restore the original textarea (with
  520. the editor's current content).</dd>
  521. </dl>
  522. <p id="defineExtension">If you want define extra methods in terms
  523. of the CodeMirror API, is it possible to
  524. use <code>CodeMirror.defineExtension(name, value)</code>. This
  525. will cause the given value (usually a method) to be added to all
  526. CodeMirror instances created from then on.</p>
  527. <h2 id="modeapi">Writing CodeMirror Modes</h2>
  528. <p>Modes typically consist of a JavaScript file and a CSS file.
  529. The CSS file (see, for
  530. example <a href="mode/javascript/javascript.css"><code>javascript.css</code></a>)
  531. defines the classes that will be used to style the syntactic
  532. elements of the code, and the script contains the logic to
  533. actually assign these classes to the right pieces of text.</p>
  534. <p>You'll usually want to use some kind of prefix for your CSS
  535. classes, so that they are unlikely to clash with other classes,
  536. both those used by other modes and those defined by the page in
  537. which CodeMirror is embedded.</p>
  538. <p id="defineMode">The mode script should
  539. call <code>CodeMirror.defineMode</code> to register itself with
  540. CodeMirror. This function takes two arguments. The first should be
  541. the name of the mode, for which you should use a lowercase string,
  542. preferably one that is also the name of the files that define the
  543. mode (i.e. <code>"xml"</code> is defined <code>xml.js</code>). The
  544. second argument should be a function that, given a CodeMirror
  545. configuration object (the thing passed to
  546. the <code>CodeMirror</code> function) and a mode configuration
  547. object (as in the <a href="#option_mode"><code>mode</code></a>
  548. option), returns a mode object.</p>
  549. <p>Typically, you should use this second argument
  550. to <code>defineMode</code> as your module scope function (modes
  551. should not leak anything into the global scope!), i.e. write your
  552. whole mode inside this function.</p>
  553. <p>The main responsibility of a mode script is <em>parsing</em>
  554. the content of the editor. Depending on the language and the
  555. amount of functionality desired, this can be done in really easy
  556. or extremely complicated ways. Some parsers can be stateless,
  557. meaning that they look at one element (<em>token</em>) of the code
  558. at a time, with no memory of what came before. Most, however, will
  559. need to remember something. This is done by using a <em>state
  560. object</em>, which is an object that can be mutated every time a
  561. new token is read.</p>
  562. <p id="startState">Modes that use a state must define
  563. a <code>startState</code> method on their mode object. This is a
  564. function of no arguments that produces a state object to be used
  565. at the start of a document.</p>
  566. <p id="token">The most important part of a mode object is
  567. its <code>token(stream, state)</code> method. All modes must
  568. define this method. It should read one token from the stream it is
  569. given as an argument, optionally update its state, and return a
  570. style string, or <code>null</code> for tokens that do not have to
  571. be styled. For your styles, you can either use the 'standard' ones
  572. defined in the themes (without the <code>cm-</code> prefix), or
  573. define your own (as the <a href="../mode/diff/index.html">diff</a>
  574. mode does) and have people include a custom theme for your
  575. mode.<p>
  576. <p id="StringStream">The stream object encapsulates a line of code
  577. (tokens may never span lines) and our current position in that
  578. line. It has the following API:</p>
  579. <dl>
  580. <dt><code>eol() → boolean</code></dt>
  581. <dd>Returns true only if the stream is at the end of the
  582. line.</dd>
  583. <dt><code>sol() → boolean</code></dt>
  584. <dd>Returns true only if the stream is at the start of the
  585. line.</dd>
  586. <dt><code>peek() → character</code></dt>
  587. <dd>Returns the next character in the stream without advancing
  588. it. Will return <code>undefined</code> at the end of the
  589. line.</dd>
  590. <dt><code>next() → character</code></dt>
  591. <dd>Returns the next character in the stream and advances it.
  592. Also returns <code>undefined</code> when no more characters are
  593. available.</dd>
  594. <dt><code>eat(match) → character</code></dt>
  595. <dd><code>match</code> can be a character, a regular expression,
  596. or a function that takes a character and returns a boolean. If
  597. the next character in the stream 'matches' the given argument,
  598. it is consumed and returned. Otherwise, <code>undefined</code>
  599. is returned.</dd>
  600. <dt><code>eatWhile(match) → boolean</code></dt>
  601. <dd>Repeatedly calls <code>eat</code> with the given argument,
  602. until it fails. Returns true if any characters were eaten.</dd>
  603. <dt><code>eatSpace() → boolean</code></dt>
  604. <dd>Shortcut for <code>eatWhile</code> when matching
  605. white-space.</dd>
  606. <dt><code>skipToEnd()</code></dt>
  607. <dd>Moves the position to the end of the line.</dd>
  608. <dt><code>skipTo(ch) → boolean</code></dt>
  609. <dd>Skips to the next occurrence of the given character, if
  610. found. Returns true if the character was found.</dd>
  611. <dt><code>match(pattern, consume, caseFold) → boolean</code></dt>
  612. <dd>Act like a
  613. multi-character <code>eat</code>—if <code>consume</code> is true
  614. or not given—or a look-ahead that doesn't update the stream
  615. position—if it is false. <code>pattern</code> can be either a
  616. string or a regular expression starting with <code>^</code>.
  617. When it is a string, <code>caseFold</code> can be set to true to
  618. make the match case-insensitive. When successfully matching a
  619. regular expression, the returned value will be the array
  620. returned by <code>match</code>, in case you need to extract
  621. matched groups.</dd>
  622. <dt><code>backUp(n)</code></dt>
  623. <dd>Backs up the stream <code>n</code> characters. Backing it up
  624. further than the start of the current token will cause things to
  625. break, so be careful.</dd>
  626. <dt><code>column() → integer</code></dt>
  627. <dd>Returns the column (taking into account tabs) at which the
  628. current token starts. Can be used to find out whether a token
  629. starts a new line.</dd>
  630. <dt><code>indentation() → integer</code></dt>
  631. <dd>Tells you how far the current line has been indented, in
  632. spaces. Corrects for tab characters.</dd>
  633. <dt><code>current() → string</code></dt>
  634. <dd>Get the string between the start of the current token and
  635. the current stream position.</dd>
  636. </dl>
  637. <p id="blankLine">By default, blank lines are simply skipped when
  638. tokenizing a document. For languages that have significant blank
  639. lines, you can define a <code>blankLine(state)</code> method on
  640. your mode that will get called whenever a blank line is passed
  641. over, so that it can update the parser state.</p>
  642. <p id="copyState">Because state object are mutated, and CodeMirror
  643. needs to keep valid versions of a state around so that it can
  644. restart a parse at any line, copies must be made of state objects.
  645. The default algorithm used is that a new state object is created,
  646. which gets all the properties of the old object. Any properties
  647. which hold arrays get a copy of these arrays (since arrays tend to
  648. be used as mutable stacks). When this is not correct, for example
  649. because a mode mutates non-array properties of its state object, a
  650. mode object should define a <code>copyState</code> method,
  651. which is given a state and should return a safe copy of that
  652. state.</p>
  653. <p id="compareStates">By default, CodeMirror will stop re-parsing
  654. a document as soon as it encounters a few lines that were
  655. highlighted the same in the old parse as in the new one. It is
  656. possible to provide an explicit way to test whether a state is
  657. equivalent to another one, which CodeMirror will use (instead of
  658. the unchanged-lines heuristic) to decide when to stop
  659. highlighting. You do this by providing
  660. a <code>compareStates</code> method on your mode object, which
  661. takes two state arguments and returns a boolean indicating whether
  662. they are equivalent. See the XML mode, which uses this to provide
  663. reliable highlighting of bad closing tags, as an example.</p>
  664. <p id="indent">If you want your mode to provide smart indentation
  665. (see <a href="#option_enterMode"><code>entermode</code></a>
  666. and <a href="#option_tabMode"><code>tabMode</code></a> when they
  667. have a value of <code>"indent"</code>), you must define
  668. an <code>indent(state, textAfter)</code> method on your mode
  669. object.</p>
  670. <p>The indentation method should inspect the given state object,
  671. and optionally the <code>textAfter</code> string, which contains
  672. the text on the line that is being indented, and return an
  673. integer, the amount of spaces to indent. It should usually take
  674. the <a href="#option_indentUnit"><code>indentUnit</code></a>
  675. option into account.</p>
  676. <p id="electricChars">Finally, a mode may define
  677. an <code>electricChars</code> property, which should hold a string
  678. containing all the characters that should trigger the behaviour
  679. described for
  680. the <a href="#option_electricChars"><code>electricChars</code></a>
  681. option.</p>
  682. <p>So, to summarize, a mode <em>must</em> provide
  683. a <code>token</code> method, and it <em>may</em>
  684. provide <code>startState</code>, <code>copyState</code>,
  685. and <code>indent</code> methods. For an example of a trivial mode,
  686. see the <a href="mode/diff/diff.js">diff mode</a>, for a more
  687. involved example, see
  688. the <a href="mode/javascript/javascript.js">JavaScript
  689. mode</a>.</p>
  690. <p>Sometimes, it is useful for modes to <em>nest</em>—to have one
  691. mode delegate work to another mode. An example of this kind of
  692. mode is the <a href="mode/htmlmixed/htmlmixed.js">mixed-mode HTML
  693. mode</a>. To implement such nesting, it is usually necessary to
  694. create mode objects and copy states yourself. To create a mode
  695. object, there are <code>CodeMirror.getMode(options,
  696. parserConfig)</code>, where the first argument is a configuration
  697. object as passed to the mode constructor function, and the second
  698. argument is a mode specification as in
  699. the <a href="#option_mode"><code>mode</code></a> option. To copy a
  700. state object, call <code>CodeMirror.copyState(mode, state)</code>,
  701. where <code>mode</code> is the mode that created the given
  702. state.</p>
  703. <p>To make indentation work properly in a nested parser, it is
  704. advisable to give the <code>startState</code> method of modes that
  705. are intended to be nested an optional argument that provides the
  706. base indentation for the block of code. The JavaScript and CSS
  707. parser do this, for example, to allow JavaScript and CSS code
  708. inside the mixed-mode HTML mode to be properly indented.</p>
  709. <p>Finally, it is possible to associate your mode, or a certain
  710. configuration of your mode, with
  711. a <a href="http://en.wikipedia.org/wiki/MIME">MIME</a> type. For
  712. example, the JavaScript mode associates itself
  713. with <code>text/javascript</code>, and its JSON variant
  714. with <code>application/json</code>. To do this,
  715. call <code>CodeMirror.defineMIME(mime, modeSpec)</code>,
  716. where <code>modeSpec</code> can be a string or object specifying a
  717. mode, as in the <a href="#option_mode"><code>mode</code></a>
  718. option.</p>
  719. </div><div class="rightsmall blk">
  720. <h2>Contents</h2>
  721. <ul>
  722. <li><a href="#overview">Overview</a></li>
  723. <li><a href="#usage">Basic Usage</a></li>
  724. <li><a href="#config">Configuration</a></li>
  725. <li><a href="#styling">Customized Styling</a></li>
  726. <li><a href="#api">Programming API</a></li>
  727. <li><a href="#modeapi">Writing CodeMirror Modes</a></li>
  728. </ul>
  729. </div></div>
  730. <div style="height: 2em">&nbsp;</div>
  731. </body>
  732. </html>