1
0

manual.html 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  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>
  17. <style>
  18. dt { text-indent: -1.5em; padding-left: 1.5em; }
  19. </style>
  20. </head>
  21. <body>
  22. <h1><span class="logo-braces">{ }</span> <a href="http://codemirror.net/">CodeMirror</a></h1>
  23. <div class="grey">
  24. <img src="baboon.png" class="logo" alt="logo">
  25. <pre>
  26. /* User manual and
  27. reference guide */
  28. </pre>
  29. </div>
  30. <div class="clear"><div class="leftbig blk">
  31. <h2 id="overview">Overview</h2>
  32. <p>CodeMirror is a code-editor component that can be embedded in
  33. Web pages. The core library provides <em>only</em> the editor
  34. component, no accompanying buttons, auto-completion, or other IDE
  35. functionality. It does provide a rich API on top of which such
  36. functionality can be straightforwardly implemented. See
  37. the <a href="#addons">add-ons</a> included in the distribution,
  38. and
  39. the <a href="https://github.com/jagthedrummer/codemirror-ui">CodeMirror
  40. UI</a> project, for reusable implementations of extra features.</p>
  41. <p>CodeMirror works with language-specific modes. Modes are
  42. JavaScript programs that help color (and optionally indent) text
  43. written in a given language. The distribution comes with a number
  44. of modes (see the <a href="../mode/"><code>mode/</code></a>
  45. directory), and it isn't hard to <a href="#modeapi">write new
  46. ones</a> for other languages.</p>
  47. <h2 id="usage">Basic Usage</h2>
  48. <p>The easiest way to use CodeMirror is to simply load the script
  49. and style sheet found under <code>lib/</code> in the distribution,
  50. plus a mode script from one of the <code>mode/</code> directories.
  51. (See <a href="compress.html">the compression helper</a> for an
  52. easy way to combine scripts.) For example:</p>
  53. <pre data-lang="text/html">&lt;script src="lib/codemirror.js">&lt;/script>
  54. &lt;link rel="stylesheet" href="../lib/codemirror.css">
  55. &lt;script src="mode/javascript/javascript.js">&lt;/script></pre>
  56. <p>Having done this, an editor instance can be created like
  57. this:</p>
  58. <pre data-lang="javascript">var myCodeMirror = CodeMirror(document.body);</pre>
  59. <p>The editor will be appended to the document body, will start
  60. empty, and will use the mode that we loaded. To have more control
  61. over the new editor, a configuration object can be passed
  62. to <a href="#CodeMirror"><code>CodeMirror</code></a> as a second
  63. argument:</p>
  64. <pre data-lang="javascript">var myCodeMirror = CodeMirror(document.body, {
  65. value: "function myScript(){return 100;}\n",
  66. mode: "javascript"
  67. });</pre>
  68. <p>This will initialize the editor with a piece of code already in
  69. it, and explicitly tell it to use the JavaScript mode (which is
  70. useful when multiple modes are loaded).
  71. See <a href="#config">below</a> for a full discussion of the
  72. configuration options that CodeMirror accepts.</p>
  73. <p>In cases where you don't want to append the editor to an
  74. element, and need more control over the way it is inserted, the
  75. first argument to the <code>CodeMirror</code> function can also
  76. be a function that, when given a DOM element, inserts it into the
  77. document somewhere. This could be used to, for example, replace a
  78. textarea with a real editor:</p>
  79. <pre data-lang="javascript">var myCodeMirror = CodeMirror(function(elt) {
  80. myTextArea.parentNode.replaceChild(elt, myTextArea);
  81. }, {value: myTextArea.value});</pre>
  82. <p>However, for this use case, which is a common way to use
  83. CodeMirror, the library provides a much more powerful
  84. shortcut:</p>
  85. <pre data-lang="javascript">var myCodeMirror = CodeMirror.fromTextArea(myTextArea);</pre>
  86. <p>This will, among other things, ensure that the textarea's value
  87. is updated with the editor's contents when the form (if it is part
  88. of a form) is submitted. See the <a href="#fromTextArea">API
  89. reference</a> for a full description of this method.</p>
  90. <h2 id="config">Configuration</h2>
  91. <p>Both the <a href="#CodeMirror"><code>CodeMirror</code></a>
  92. function and its <code>fromTextArea</code> method take as second
  93. (optional) argument an object containing configuration options.
  94. Any option not supplied like this will be taken
  95. from <a href="#defaults"><code>CodeMirror.defaults</code></a>, an
  96. object containing the default options. You can update this object
  97. to change the defaults on your page.</p>
  98. <p>Options are not checked in any way, so setting bogus option
  99. values is bound to lead to odd errors.</p>
  100. <p>These are the supported options:</p>
  101. <dl>
  102. <dt id="option_value"><code><strong>value</strong>: string|CodeMirror.Doc</code></dt>
  103. <dd>The starting value of the editor. Can be a string, or
  104. a <a href="#api_doc">document object</a>.</dd>
  105. <dt id="option_mode"><code><strong>mode</strong>: string|object</code></dt>
  106. <dd>The mode to use. When not given, this will default to the
  107. first mode that was loaded. It may be a string, which either
  108. simply names the mode or is
  109. a <a href="http://en.wikipedia.org/wiki/MIME">MIME</a> type
  110. associated with the mode. Alternatively, it may be an object
  111. containing configuration options for the mode, with
  112. a <code>name</code> property that names the mode (for
  113. example <code>{name: "javascript", json: true}</code>). The demo
  114. pages for each mode contain information about what configuration
  115. parameters the mode supports. You can ask CodeMirror which modes
  116. and MIME types have been defined by inspecting
  117. the <code>CodeMirror.modes</code>
  118. and <code>CodeMirror.mimeModes</code> objects. The first maps
  119. mode names to their constructors, and the second maps MIME types
  120. to mode specs.</dd>
  121. <dt id="option_theme"><code><strong>theme</strong>: string</code></dt>
  122. <dd>The theme to style the editor with. You must make sure the
  123. CSS file defining the corresponding <code>.cm-s-[name]</code>
  124. styles is loaded (see
  125. the <a href="../theme/"><code>theme</code></a> directory in the
  126. distribution). The default is <code>"default"</code>, for which
  127. colors are included in <code>codemirror.css</code>. It is
  128. possible to use multiple theming classes at once—for
  129. example <code>"foo bar"</code> will assign both
  130. the <code>cm-s-foo</code> and the <code>cm-s-bar</code> classes
  131. to the editor.</dd>
  132. <dt id="option_indentUnit"><code><strong>indentUnit</strong>: integer</code></dt>
  133. <dd>How many spaces a block (whatever that means in the edited
  134. language) should be indented. The default is 2.</dd>
  135. <dt id="option_smartIndent"><code><strong>smartIndent</strong>: boolean</code></dt>
  136. <dd>Whether to use the context-sensitive indentation that the
  137. mode provides (or just indent the same as the line before).
  138. Defaults to true.</dd>
  139. <dt id="option_tabSize"><code><strong>tabSize</strong>: integer</code></dt>
  140. <dd>The width of a tab character. Defaults to 4.</dd>
  141. <dt id="option_indentWithTabs"><code><strong>indentWithTabs</strong>: boolean</code></dt>
  142. <dd>Whether, when indenting, the first N*<code>tabSize</code>
  143. spaces should be replaced by N tabs. Default is false.</dd>
  144. <dt id="option_electricChars"><code><strong>electricChars</strong>: boolean</code></dt>
  145. <dd>Configures whether the editor should re-indent the current
  146. line when a character is typed that might change its proper
  147. indentation (only works if the mode supports indentation).
  148. Default is true.</dd>
  149. <dt id="option_rtlMoveVisually"><code><strong>rtlMoveVisually</strong>: boolean</code></dt>
  150. <dd>Determines whether horizontal cursor movement through
  151. right-to-left (Arabic, Hebrew) text is visual (pressing the left
  152. arrow moves the cursor left) or logical (pressing the left arrow
  153. moves to the next lower index in the string, which is visually
  154. right in right-to-left text). The default is <code>false</code>
  155. on Windows, and <code>true</code> on other platforms.</dd>
  156. <dt id="option_keyMap"><code><strong>keyMap</strong>: string</code></dt>
  157. <dd>Configures the keymap to use. The default
  158. is <code>"default"</code>, which is the only keymap defined
  159. in <code>codemirror.js</code> itself. Extra keymaps are found in
  160. the <a href="../keymap/"><code>keymap</code></a> directory. See
  161. the <a href="#keymaps">section on keymaps</a> for more
  162. information.</dd>
  163. <dt id="option_extraKeys"><code><strong>extraKeys</strong>: object</code></dt>
  164. <dd>Can be used to specify extra keybindings for the editor,
  165. alongside the ones defined
  166. by <a href="#option_keyMap"><code>keyMap</code></a>. Should be
  167. either null, or a valid <a href="#keymaps">keymap</a> value.</dd>
  168. <dt id="option_lineWrapping"><code><strong>lineWrapping</strong>: boolean</code></dt>
  169. <dd>Whether CodeMirror should scroll or wrap for long lines.
  170. Defaults to <code>false</code> (scroll).</dd>
  171. <dt id="option_lineNumbers"><code><strong>lineNumbers</strong>: boolean</code></dt>
  172. <dd>Whether to show line numbers to the left of the editor.</dd>
  173. <dt id="option_firstLineNumber"><code><strong>firstLineNumber</strong>: integer</code></dt>
  174. <dd>At which number to start counting lines. Default is 1.</dd>
  175. <dt id="option_lineNumberFormatter"><code><strong>lineNumberFormatter</strong>: function(line: integer) → string</code></dt>
  176. <dd>A function used to format line numbers. The function is
  177. passed the line number, and should return a string that will be
  178. shown in the gutter.</dd>
  179. <dt id="option_gutters"><code><strong>gutters</strong>: array&lt;string&gt;</code></dt>
  180. <dd>Can be used to add extra gutters (beyond or instead of the
  181. line number gutter). Should be an array of CSS class names, each
  182. of which defines a <code>width</code> (and optionally a
  183. background), and which will be used to draw the background of
  184. the gutters. <em>May</em> include
  185. the <code>CodeMirror-linenumbers</code> class, in order to
  186. explicitly set the position of the line number gutter (it will
  187. default to be to the right of all other gutters). These class
  188. names are the keys passed
  189. to <a href="#setGutterMarker"><code>setGutterMarker</code></a>.</dd>
  190. <dt id="option_fixedGutter"><code><strong>fixedGutter</strong>: boolean</code></dt>
  191. <dd>Determines whether the gutter scrolls along with the content
  192. horizontally (false) or whether it stays fixed during horizontal
  193. scrolling (true, the default).</dd>
  194. <dt id="option_coverGutterNextToScrollbar"><code><strong>coverGutterNextToScrollbar</strong>: boolean</code></dt>
  195. <dd>When <a href="#option_fixedGutter"><code>fixedGutter</code></a>
  196. is on, and there is a horizontal scrollbar, by default the
  197. gutter will be visible to the left of this scrollbar. If this
  198. option is set to true, it will be covered by an element with
  199. class <code>CodeMirror-gutter-filler</code>.</dd>
  200. <dt id="option_readOnly"><code><strong>readOnly</strong>: boolean|string</code></dt>
  201. <dd>This disables editing of the editor content by the user. If
  202. the special value <code>"nocursor"</code> is given (instead of
  203. simply <code>true</code>), focusing of the editor is also
  204. disallowed.</dd>
  205. <dt id="option_showCursorWhenSelecting"><code><strong>showCursorWhenSelecting</strong>: boolean</code></dt>
  206. <dd>Whether the cursor should be drawn when a selection is
  207. active. Defaults to false.</dd>
  208. <dt id="option_undoDepth"><code><strong>undoDepth</strong>: integer</code></dt>
  209. <dd>The maximum number of undo levels that the editor stores.
  210. Defaults to 40.</dd>
  211. <dt id="option_historyEventDelay"><code><strong>historyEventDelay</strong>: integer</code></dt>
  212. <dd>The period of inactivity (in milliseconds) that will cause a
  213. new history event to be started when typing or deleting.
  214. Defaults to 500.</dd>
  215. <dt id="option_tabindex"><code><strong>tabindex</strong>: integer</code></dt>
  216. <dd>The <a href="http://www.w3.org/TR/html401/interact/forms.html#adef-tabindex">tab
  217. index</a> to assign to the editor. If not given, no tab index
  218. will be assigned.</dd>
  219. <dt id="option_autofocus"><code><strong>autofocus</strong>: boolean</code></dt>
  220. <dd>Can be used to make CodeMirror focus itself on
  221. initialization. Defaults to off.
  222. When <a href="#fromTextArea"><code>fromTextArea</code></a> is
  223. used, and no explicit value is given for this option, it will be
  224. set to true when either the source textarea is focused, or it
  225. has an <code>autofocus</code> attribute and no other element is
  226. focused.</dd>
  227. </dl>
  228. <p>Below this a few more specialized, low-level options are
  229. listed. These are only useful in very specific situations, you
  230. might want to skip them the first time you read this manual.</p>
  231. <dl>
  232. <dt id="option_dragDrop"><code><strong>dragDrop</strong>: boolean</code></dt>
  233. <dd>Controls whether drag-and-drop is enabled. On by default.</dd>
  234. <dt id="option_onDragEvent"><code><strong>onDragEvent</strong>: function(instance: CodeMirror, event: Event) → boolean</code></dt>
  235. <dd>When given, this will be called when the editor is handling
  236. a <code>dragenter</code>, <code>dragover</code>,
  237. or <code>drop</code> event. It will be passed the editor instance
  238. and the event object as arguments. The callback can choose to
  239. handle the event itself, in which case it should
  240. return <code>true</code> to indicate that CodeMirror should not
  241. do anything further.</dd>
  242. <dt id="option_onKeyEvent"><code><strong>onKeyEvent</strong>: function(instance: CodeMirror, event: Event) → boolean</code></dt>
  243. <dd>This provides a rather low-level hook into CodeMirror's key
  244. handling. If provided, this function will be called on
  245. every <code>keydown</code>, <code>keyup</code>,
  246. and <code>keypress</code> event that CodeMirror captures. It
  247. will be passed two arguments, the editor instance and the key
  248. event. This key event is pretty much the raw key event, except
  249. that a <code>stop()</code> method is always added to it. You
  250. could feed it to, for example, <code>jQuery.Event</code> to
  251. further normalize it.<br>This function can inspect the key
  252. event, and handle it if it wants to. It may return true to tell
  253. CodeMirror to ignore the event. Be wary that, on some browsers,
  254. stopping a <code>keydown</code> does not stop
  255. the <code>keypress</code> from firing, whereas on others it
  256. does. If you respond to an event, you should probably inspect
  257. its <code>type</code> property and only do something when it
  258. is <code>keydown</code> (or <code>keypress</code> for actions
  259. that need character data).</dd>
  260. <dt id="option_cursorBlinkRate"><code><strong>cursorBlinkRate</strong>: number</code></dt>
  261. <dd>Half-period in milliseconds used for cursor blinking. The default blink
  262. rate is 530ms.</dd>
  263. <dt id="option_cursorScrollMargin"><code><strong>cursorScrollMargin</strong>: number</code></dt>
  264. <dd>How much extra space to always keep above and below the
  265. cursor when approaching the top or bottom of the visible view in
  266. a scrollable document. Default is 0.</dd>
  267. <dt id="option_cursorHeight"><code><strong>cursorHeight</strong>: number</code></dt>
  268. <dd>Determines the height of the cursor. Default is 1, meaning
  269. it spans the whole height of the line. For some fonts (and by
  270. some tastes) a smaller height (for example <code>0.85</code>),
  271. which causes the cursor to not reach all the way to the bottom
  272. of the line, looks better</dd>
  273. <dt id="option_workTime"><code><strong>workTime</strong>, <strong>workDelay</strong>: number</code></dt>
  274. <dd>Highlighting is done by a pseudo background-thread that will
  275. work for <code>workTime</code> milliseconds, and then use
  276. timeout to sleep for <code>workDelay</code> milliseconds. The
  277. defaults are 200 and 300, you can change these options to make
  278. the highlighting more or less aggressive.</dd>
  279. <dt id="option_workDelay"><code><strong>workDelay</strong>: number</code></dt>
  280. <dd>See <a href="#option_workTime"><code>workTime</code></a>.</dd>
  281. <dt id="option_pollInterval"><code><strong>pollInterval</strong>: number</code></dt>
  282. <dd>Indicates how quickly CodeMirror should poll its input
  283. textarea for changes (when focused). Most input is captured by
  284. events, but some things, like IME input on some browsers, don't
  285. generate events that allow CodeMirror to properly detect it.
  286. Thus, it polls. Default is 100 milliseconds.</dd>
  287. <dt id="option_flattenSpans"><code><strong>flattenSpans</strong>: boolean</code></dt>
  288. <dd>By default, CodeMirror will combine adjacent tokens into a
  289. single span if they have the same class. This will result in a
  290. simpler DOM tree, and thus perform better. With some kinds of
  291. styling (such as rounded corners), this will change the way the
  292. document looks. You can set this option to false to disable this
  293. behavior.</dd>
  294. <dt id="option_maxHighlightLength"><code><strong>maxHighlightLength</strong>: number</code></dt>
  295. <dd>When highlighting long lines, in order to stay responsive,
  296. the editor will give up and simply style the rest of the line as
  297. plain text when it reaches a certain position. The default is
  298. 10000. You can set this to <code>Infinity</code> to turn off
  299. this behavior.</dd>
  300. <dt id="option_viewportMargin"><code><strong>viewportMargin</strong>: integer</code></dt>
  301. <dd>Specifies the amount of lines that are rendered above and
  302. below the part of the document that's currently scrolled into
  303. view. This affects the amount of updates needed when scrolling,
  304. and the amount of work that such an update does. You should
  305. usually leave it at its default, 10. Can be set
  306. to <code>Infinity</code> to make sure the whole document is
  307. always rendered, and thus the browser's text search works on it.
  308. This <em>will</em> have bad effects on performance of big
  309. documents.</dd>
  310. </dl>
  311. <h2 id="events">Events</h2>
  312. <p>A CodeMirror instance emits a number of events, which allow
  313. client code to react to various situations. These are registered
  314. with the <a href="#on"><code>on</code></a> method (and
  315. removed with the <a href="#off"><code>off</code></a>
  316. method). These are the events that fire on the instance object.
  317. The name of the event is followed by the arguments that will be
  318. passed to the handler. The <code>instance</code> argument always
  319. refers to the editor instance.</p>
  320. <dl>
  321. <dt id="event_change"><code><strong>"change"</strong> (instance: CodeMirror, changeObj: object)</code></dt>
  322. <dd>Fires every time the content of the editor is changed.
  323. The <code>changeObj</code> is a <code>{from, to, text, removed,
  324. next}</code> object containing information about the changes
  325. that occurred as second argument. <code>from</code>
  326. and <code>to</code> are the positions (in the pre-change
  327. coordinate system) where the change started and ended (for
  328. example, it might be <code>{ch:0, line:18}</code> if the
  329. position is at the beginning of line #19). <code>text</code> is
  330. an array of strings representing the text that replaced the
  331. changed range (split by line). <code>removed</code> is the text
  332. that used to be between <code>from</code> and <code>to</code>,
  333. which is overwritten by this change. If multiple changes
  334. happened during a single operation, the object will have
  335. a <code>next</code> property pointing to another change object
  336. (which may point to another, etc).</dd>
  337. <dt id="event_beforeChange"><code><strong>"beforeChange"</strong> (instance: CodeMirror, changeObj: object)</code></dt>
  338. <dd>This event is fired before a change is applied, and its
  339. handler may choose to modify or cancel the change.
  340. The <code>changeObj</code> object
  341. has <code>from</code>, <code>to</code>, and <code>text</code>
  342. properties, as with
  343. the <a href="#event_change"><code>"change"</code></a> event, but
  344. never a <code>next</code> property, since this is fired for each
  345. individual change, and not batched per operation. It also
  346. has <code>update(from, to, text)</code>
  347. and <code>cancel()</code> methods, which may be used to modify
  348. or cancel the change. All three arguments to <code>update</code>
  349. are optional, and can be left off to leave the existing value
  350. for that field intact. <strong>Note:</strong> you may not do
  351. anything from a <code>"beforeChange"</code> handler that would
  352. cause changes to the document or its visualization. Doing so
  353. will, since this handler is called directly from the bowels of
  354. the CodeMirror implementation, probably cause the editor to
  355. become corrupted.</dd>
  356. <dt id="event_cursorActivity"><code><strong>"cursorActivity"</strong> (instance: CodeMirror)</code></dt>
  357. <dd>Will be fired when the cursor or selection moves, or any
  358. change is made to the editor content.</dd>
  359. <dt id="event_beforeSelectionChange"><code><strong>"beforeSelectionChange"</strong> (instance: CodeMirror, selection: {head, anchor})</code></dt>
  360. <dd>This event is fired before the selection is moved. Its
  361. handler may modify the resulting selection head and anchor.
  362. The <code>selection</code> parameter is an object
  363. with <code>head</code> and <code>anchor</code> properties
  364. holding <code>{line, ch}</code> objects, which the handler can
  365. read and update. Handlers for this event have the same
  366. restriction
  367. as <a href="#event_beforeChange"><code>"beforeChange"</code></a>
  368. handlers — they should not do anything to directly update the
  369. state of the editor.</dd>
  370. <dt id="event_viewportChange"><code><strong>"viewportChange"</strong> (instance: CodeMirror, from: number, to: number)</code></dt>
  371. <dd>Fires whenever the <a href="#getViewport">view port</a> of
  372. the editor changes (due to scrolling, editing, or any other
  373. factor). The <code>from</code> and <code>to</code> arguments
  374. give the new start and end of the viewport.</dd>
  375. <dt id="event_gutterClick"><code><strong>"gutterClick"</strong> (instance: CodeMirror, line: integer, gutter: string, clickEvent: Event)</code></dt>
  376. <dd>Fires when the editor gutter (the line-number area) is
  377. clicked. Will pass the editor instance as first argument, the
  378. (zero-based) number of the line that was clicked as second
  379. argument, the CSS class of the gutter that was clicked as third
  380. argument, and the raw <code>mousedown</code> event object as
  381. fourth argument.</dd>
  382. <dt id="event_focus"><code><strong>"focus"</strong> (instance: CodeMirror)</code></dt>
  383. <dd>Fires whenever the editor is focused.</dd>
  384. <dt id="event_blur"><code><strong>"blur"</strong> (instance: CodeMirror)</code></dt>
  385. <dd>Fires whenever the editor is unfocused.</dd>
  386. <dt id="event_scroll"><code><strong>"scroll"</strong> (instance: CodeMirror)</code></dt>
  387. <dd>Fires when the editor is scrolled.</dd>
  388. <dt id="event_update"><code><strong>"update"</strong> (instance: CodeMirror)</code></dt>
  389. <dd>Will be fired whenever CodeMirror updates its DOM display.</dd>
  390. <dt id="event_renderLine"><code><strong>"renderLine"</strong> (instance: CodeMirror, line: LineHandle, element: Element)</code></dt>
  391. <dd>Fired whenever a line is (re-)rendered to the DOM. Fired
  392. right after the DOM element is built, <em>before</em> it is
  393. added to the document. The handler may mess with the style of
  394. the resulting element, or add event handlers, but
  395. should <em>not</em> try to change the state of the editor.</dd>
  396. </dl>
  397. <p>It is also possible to <a href="#on">register</a> events on
  398. other objects. Use <code>CodeMirror.on(handle, "eventName",
  399. func)</code> to register handlers on objects that don't have their
  400. own <code>on</code> method. Document objects (instances
  401. of <a href="#Doc"><code>CodeMirror.Doc</code></a>) emit the
  402. following events:</p>
  403. <dl>
  404. <dt id="event_doc_change"><code><strong>"change"</strong> (doc: CodeMirror.Doc, changeObj: object)</code></dt>
  405. <dd>Fired whenever a change occurs to the
  406. document. <code>changeObj</code> has a similar type as the
  407. object passed to the
  408. editor's <a href="#event_change"><code>"change"</code></a>
  409. event, but it never has a <code>next</code> property, because
  410. document change events are not batched (whereas editor change
  411. events are).</dd>
  412. <dt id="event_doc_beforeChange"><code><strong>"beforeChange"</strong> (doc: CodeMirror.Doc, change: object)</code></dt>
  413. <dd>See the <a href="#event_beforeChange">description of the
  414. same event</a> on editor instances.</dd>
  415. <dt id="event_doc_cursorActivity"><code><strong>"cursorActivity"</strong> (doc: CodeMirror.Doc)</code></dt>
  416. <dd>Fired whenever the cursor or selection in this document
  417. changes.</dd>
  418. <dt id="event_doc_beforeSelectionChange"><code><strong>"beforeSelectionChange"</strong> (doc: CodeMirror.Doc, selection: {head, anchor})</code></dt>
  419. <dd>Equivalent to
  420. the <a href="#event_beforeSelectionChange">event by the same
  421. name</a> as fired on editor instances.</dd>
  422. </dl>
  423. <p>Line handles (as returned by, for
  424. example, <a href="#getLineHandle"><code>getLineHandle</code></a>)
  425. support these events:</p>
  426. <dl>
  427. <dt id="event_delete"><code><strong>"delete"</strong> ()</code></dt>
  428. <dd>Will be fired when the line object is deleted. A line object
  429. is associated with the <em>start</em> of the line. Mostly useful
  430. when you need to find out when your <a href="#setGutterMarker">gutter
  431. markers</a> on a given line are removed.</dd>
  432. <dt id="event_line_change"><code><strong>"change"</strong> (line: LineHandle, changeObj: object)</code></dt>
  433. <dd>Fires when the line's text content is changed in any way
  434. (but the line is not deleted outright). The <code>change</code>
  435. object is similar to the one passed
  436. to <a href="#event_change">change event</a> on the editor
  437. object.</dd>
  438. </dl>
  439. <p>Marked range handles (<code>CodeMirror.TextMarker</code>), as returned
  440. by <a href="#markText"><code>markText</code></a>
  441. and <a href="#setBookmark"><code>setBookmark</code></a>, emit the
  442. following events:</p>
  443. <dl>
  444. <dt id="event_beforeCursorEnter"><code><strong>"beforeCursorEnter"</strong> ()</code></dt>
  445. <dd>Fired when the cursor enters the marked range. From this
  446. event handler, the editor state may be inspected
  447. but <em>not</em> modified, with the exception that the range on
  448. which the event fires may be cleared.</dd>
  449. <dt id="event_clear"><code><strong>"clear"</strong> ()</code></dt>
  450. <dd>Fired when the range is cleared, either through cursor
  451. movement in combination
  452. with <a href="#mark_clearOnEnter"><code>clearOnEnter</code></a>
  453. or through a call to its <code>clear()</code> method. Will only
  454. be fired once per handle. Note that deleting the range through
  455. text editing does not fire this event, because an undo
  456. action might bring the range back into existence.</dd>
  457. <dt id="event_hide"><code><strong>"hide"</strong> ()</code></dt>
  458. <dd>Fired when the last part of the marker is removed from the
  459. document by editing operations.</dd>
  460. <dt id="event_unhide"><code><strong>"unhide"</strong> ()</code></dt>
  461. <dd>Fired when, after the marker was removed by editing, a undo
  462. operation brought the marker back.</dd>
  463. </dl>
  464. <p>Line widgets (<code>CodeMirror.LineWidget</code>), returned
  465. by <a href="#addLineWidget"><code>addLineWidget</code></a>, fire
  466. these events:</p>
  467. <dl>
  468. <dt id="event_redraw"><code><strong>"redraw"</strong> ()</code></dt>
  469. <dd>Fired whenever the editor re-adds the widget to the DOM.
  470. This will happen once right after the widget is added (if it is
  471. scrolled into view), and then again whenever it is scrolled out
  472. of view and back in again, or when changes to the editor options
  473. or the line the widget is on require the widget to be
  474. redrawn.</dd>
  475. </dl>
  476. <h2 id="keymaps">Keymaps</h2>
  477. <p>Keymaps are ways to associate keys with functionality. A keymap
  478. is an object mapping strings that identify the keys to functions
  479. that implement their functionality.</p>
  480. <p>Keys are identified either by name or by character.
  481. The <code>CodeMirror.keyNames</code> object defines names for
  482. common keys and associates them with their key codes. Examples of
  483. names defined here are <code>Enter</code>, <code>F5</code>,
  484. and <code>Q</code>. These can be prefixed
  485. with <code>Shift-</code>, <code>Cmd-</code>, <code>Ctrl-</code>,
  486. and <code>Alt-</code> (in that order!) to specify a modifier. So
  487. for example, <code>Shift-Ctrl-Space</code> would be a valid key
  488. identifier.</p>
  489. <p>Common example: map the Tab key to insert spaces instead of a tab
  490. character.</p>
  491. <pre data-lang="javascript">
  492. {
  493. Tab: function(cm) {
  494. var spaces = Array(cm.getOption("indentUnit") + 1).join(" ");
  495. cm.replaceSelection(spaces, "end", "+input");
  496. }
  497. }</pre>
  498. <p>Alternatively, a character can be specified directly by
  499. surrounding it in single quotes, for example <code>'$'</code>
  500. or <code>'q'</code>. Due to limitations in the way browsers fire
  501. key events, these may not be prefixed with modifiers.</p>
  502. <p>The <code>CodeMirror.keyMap</code> object associates keymaps
  503. with names. User code and keymap definitions can assign extra
  504. properties to this object. Anywhere where a keymap is expected, a
  505. string can be given, which will be looked up in this object. It
  506. also contains the <code>"default"</code> keymap holding the
  507. default bindings.</p>
  508. <p id="commands">The values of properties in keymaps can be either functions of
  509. a single argument (the CodeMirror instance), strings, or
  510. <code>false</code>. Such strings refer to properties of the
  511. <code>CodeMirror.commands</code> object, which defines a number of
  512. common commands that are used by the default keybindings, and maps
  513. them to functions. If the property is set to <code>false</code>,
  514. CodeMirror leaves handling of the key up to the browser. A key
  515. handler function may return <code>CodeMirror.Pass</code> to indicate
  516. that it has decided not to handle the key, and other handlers (or
  517. the default behavior) should be given a turn.</p>
  518. <p>Keys mapped to command names that start with the
  519. characters <code>"go"</code> (which should be used for
  520. cursor-movement actions) will be fired even when an
  521. extra <code>Shift</code> modifier is present (i.e. <code>"Up":
  522. "goLineUp"</code> matches both up and shift-up). This is used to
  523. easily implement shift-selection.</p>
  524. <p>Keymaps can defer to each other by defining
  525. a <code>fallthrough</code> property. This indicates that when a
  526. key is not found in the map itself, one or more other maps should
  527. be searched. It can hold either a single keymap or an array of
  528. keymaps.</p>
  529. <p>When a keymap contains a <code>nofallthrough</code> property
  530. set to <code>true</code>, keys matched against that map will be
  531. ignored if they don't match any of the bindings in the map (no
  532. further child maps will be tried, and the default effect of
  533. inserting a character will not occur).</p>
  534. <h2 id="styling">Customized Styling</h2>
  535. <p>Up to a certain extent, CodeMirror's look can be changed by
  536. modifying style sheet files. The style sheets supplied by modes
  537. simply provide the colors for that mode, and can be adapted in a
  538. very straightforward way. To style the editor itself, it is
  539. possible to alter or override the styles defined
  540. in <a href="../lib/codemirror.css"><code>codemirror.css</code></a>.</p>
  541. <p>Some care must be taken there, since a lot of the rules in this
  542. file are necessary to have CodeMirror function properly. Adjusting
  543. colors should be safe, of course, and with some care a lot of
  544. other things can be changed as well. The CSS classes defined in
  545. this file serve the following roles:</p>
  546. <dl>
  547. <dt id="class_CodeMirror"><code><strong>CodeMirror</strong></code></dt>
  548. <dd>The outer element of the editor. This should be used for the
  549. editor width, height, borders and positioning. Can also be used
  550. to set styles that should hold for everything inside the editor
  551. (such as font and font size), or to set a background.</dd>
  552. <dt id="class_CodeMirror_scroll"><code><strong>CodeMirror-scroll</strong></code></dt>
  553. <dd>Whether the editor scrolls (<code>overflow: auto</code> +
  554. fixed height). By default, it does. Setting
  555. the <code>CodeMirror</code> class to have <code>height:
  556. auto</code> and giving this class <code>overflow-x: auto;
  557. overflow-y: hidden;</code> will cause the editor
  558. to <a href="../demo/resize.html">resize to fit its
  559. content</a>.</dd>
  560. <dt id="class_CodeMirror_focused"><code><strong>CodeMirror-focused</strong></code></dt>
  561. <dd>Whenever the editor is focused, the top element gets this
  562. class. This is used to hide the cursor and give the selection a
  563. different color when the editor is not focused.</dd>
  564. <dt id="class_CodeMirror_gutters"><code><strong>CodeMirror-gutters</strong></code></dt>
  565. <dd>This is the backdrop for all gutters. Use it to set the
  566. default gutter background color, and optionally add a border on
  567. the right of the gutters.</dd>
  568. <dt id="class_CodeMirror_linenumbers"><code><strong>CodeMirror-linenumbers</strong></code></dt>
  569. <dd>Use this for giving a background or width to the line number
  570. gutter.</dd>
  571. <dt id="class_CodeMirror_linenumber"><code><strong>CodeMirror-linenumber</strong></code></dt>
  572. <dd>Used to style the actual individual line numbers. These
  573. won't be children of the <code>CodeMirror-linenumbers</code>
  574. (plural) element, but rather will be absolutely positioned to
  575. overlay it. Use this to set alignment and text properties for
  576. the line numbers.</dd>
  577. <dt id="class_CodeMirror_lines"><code><strong>CodeMirror-lines</strong></code></dt>
  578. <dd>The visible lines. This is where you specify vertical
  579. padding for the editor content.</dd>
  580. <dt id="class_CodeMirror_cursor"><code><strong>CodeMirror-cursor</strong></code></dt>
  581. <dd>The cursor is a block element that is absolutely positioned.
  582. You can make it look whichever way you want.</dd>
  583. <dt id="class_CodeMirror_selected"><code><strong>CodeMirror-selected</strong></code></dt>
  584. <dd>The selection is represented by <code>span</code> elements
  585. with this class.</dd>
  586. <dt id="class_CodeMirror_matchingbracket"><code><strong>CodeMirror-matchingbracket</strong></code>,
  587. <code>CodeMirror-nonmatchingbracket</code></dt>
  588. <dd>These are used to style matched (or unmatched) brackets.</dd>
  589. </dl>
  590. <p>If your page's style sheets do funky things to
  591. all <code>div</code> or <code>pre</code> elements (you probably
  592. shouldn't do that), you'll have to define rules to cancel these
  593. effects out again for elements under the <code>CodeMirror</code>
  594. class.</p>
  595. <p>Themes are also simply CSS files, which define colors for
  596. various syntactic elements. See the files in
  597. the <a href="../theme/"><code>theme</code></a> directory.</p>
  598. <h2 id="api">Programming API</h2>
  599. <p>A lot of CodeMirror features are only available through its
  600. API. Thus, you need to write code (or
  601. use <a href="#addons">add-ons</a>) if you want to expose them to
  602. your users.</p>
  603. <p>Whenever points in the document are represented, the API uses
  604. objects with <code>line</code> and <code>ch</code> properties.
  605. Both are zero-based. CodeMirror makes sure to 'clip' any positions
  606. passed by client code so that they fit inside the document, so you
  607. shouldn't worry too much about sanitizing your coordinates. If you
  608. give <code>ch</code> a value of <code>null</code>, or don't
  609. specify it, it will be replaced with the length of the specified
  610. line.</p>
  611. <p>Methods prefixed with <code>doc.</code> can, unless otherwise
  612. specified, be called both on <code>CodeMirror</code> (editor)
  613. instances and <code>CodeMirror.Doc</code> instances. Methods
  614. prefixed with <code>cm.</code> are <em>only</em> available
  615. on <code>CodeMirror</code> instances.</p>
  616. <h3 id="api_constructor">Constructor</h3>
  617. <p id="CodeMirror">Constructing an editor instance is done with
  618. the <code><strong>CodeMirror</strong>(place: Element|fn(Element),
  619. ?option: object)</code> constructor. If the <code>place</code>
  620. argument is a DOM element, the editor will be appended to it. If
  621. it is a function, it will be called, and is expected to place the
  622. editor into the document. <code>options</code> may be an element
  623. mapping <a href="#config">option names</a> to values. The options
  624. that it doesn't explicitly specify (or all options, if it is not
  625. passed) will be taken
  626. from <a href="#defaults"><code>CodeMirror.defaults</code></a>.</p>
  627. <p>Note that the options object passed to the constructor will be
  628. mutated when the instance's options
  629. are <a href="#setOption">changed</a>, so you shouldn't share such
  630. objects between instances.</p>
  631. <p>See <a href="#fromTextArea"><code>CodeMirror.fromTextArea</code></a>
  632. for another way to construct an editor instance.</p>
  633. <h3 id="api_content">Content manipulation methods</h3>
  634. <dl>
  635. <dt id="getValue"><code><strong>doc.getValue</strong>(?seperator: string) → string</code></dt>
  636. <dd>Get the current editor content. You can pass it an optional
  637. argument to specify the string to be used to separate lines
  638. (defaults to <code>"\n"</code>).</dd>
  639. <dt id="setValue"><code><strong>doc.setValue</strong>(content: string)</code></dt>
  640. <dd>Set the editor content.</dd>
  641. <dt id="getRange"><code><strong>doc.getRange</strong>(from: {line, ch}, to: {line, ch}, ?seperator: string) → string</code></dt>
  642. <dd>Get the text between the given points in the editor, which
  643. should be <code>{line, ch}</code> objects. An optional third
  644. argument can be given to indicate the line separator string to
  645. use (defaults to <code>"\n"</code>).</dd>
  646. <dt id="replaceRange"><code><strong>doc.replaceRange</strong>(replacement: string, from: {line, ch}, to: {line, ch})</code></dt>
  647. <dd>Replace the part of the document between <code>from</code>
  648. and <code>to</code> with the given string. <code>from</code>
  649. and <code>to</code> must be <code>{line, ch}</code>
  650. objects. <code>to</code> can be left off to simply insert the
  651. string at position <code>from</code>.</dd>
  652. <dt id="getLine"><code><strong>doc.getLine</strong>(n: integer) → string</code></dt>
  653. <dd>Get the content of line <code>n</code>.</dd>
  654. <dt id="setLine"><code><strong>doc.setLine</strong>(n: integer, text: string)</code></dt>
  655. <dd>Set the content of line <code>n</code>.</dd>
  656. <dt id="removeLine"><code><strong>doc.removeLine</strong>(n: integer)</code></dt>
  657. <dd>Remove the given line from the document.</dd>
  658. <dt id="lineCount"><code><strong>doc.lineCount</strong>() → integer</code></dt>
  659. <dd>Get the number of lines in the editor.</dd>
  660. <dt id="firstLine"><code><strong>doc.firstLine</strong>() → integer</code></dt>
  661. <dd>Get the first line of the editor. This will
  662. usually be zero but for <a href="#linkedDoc_from">linked sub-views</a>,
  663. or <a href="#api_doc">documents</a> instantiated with a non-zero
  664. first line, it might return other values.</dd>
  665. <dt id="lastLine"><code><strong>doc.lastLine</strong>() → integer</code></dt>
  666. <dd>Get the last line of the editor. This will
  667. usually be <code>doc.lineCount() - 1</code>,
  668. but for <a href="#linkedDoc_from">linked sub-views</a>,
  669. it might return other values.</dd>
  670. <dt id="getLineHandle"><code><strong>doc.getLineHandle</strong>(num: integer) → LineHandle</code></dt>
  671. <dd>Fetches the line handle for the given line number.</dd>
  672. <dt id="getLineNumber"><code><strong>doc.getLineNumber</strong>(handle: LineHandle) → integer</code></dt>
  673. <dd>Given a line handle, returns the current position of that
  674. line (or <code>null</code> when it is no longer in the
  675. document).</dd>
  676. <dt id="eachLine"><code><strong>doc.eachLine</strong>(f: (line: LineHandle))</code></dt>
  677. <dt><code><strong>doc.eachLine</strong>(start: integer, end: integer, f: (line: LineHandle))</code></dt>
  678. <dd>Iterate over the whole document, or if <code>start</code>
  679. and <code>end</code> line numbers are given, the range
  680. from <code>start</code> up to (not including) <code>end</code>,
  681. and call <code>f</code> for each line, passing the line handle.
  682. This is a faster way to visit a range of line handlers than
  683. calling <a href="#getLineHandle"><code>getLineHandle</code></a>
  684. for each of them. Note that line handles have
  685. a <code>text</code> property containing the line's content (as a
  686. string).</dd>
  687. <dt id="markClean"><code><strong>doc.markClean</strong>()</code></dt>
  688. <dd>Set the editor content as 'clean', a flag that it will
  689. retain until it is edited, and which will be set again when such
  690. an edit is undone again. Useful to track whether the content
  691. needs to be saved.</dd>
  692. <dt id="isClean"><code><strong>doc.isClean</strong>() → boolean</code></dt>
  693. <dd>Returns whether the document is currently clean (not
  694. modified since initialization or the last call
  695. to <a href="#markClean"><code>markClean</code></a>).</dd>
  696. </dl>
  697. <h3 id="api_selection">Cursor and selection methods</h3>
  698. <dl>
  699. <dt id="getSelection"><code><strong>doc.getSelection</strong>() → string</code></dt>
  700. <dd>Get the currently selected code.</dd>
  701. <dt id="replaceSelection"><code><strong>doc.replaceSelection</strong>(replacement: string, ?collapse: string)</code></dt>
  702. <dd>Replace the selection with the given string. By default, the
  703. new selection will span the inserted text. The
  704. optional <code>collapse</code> argument can be used to change
  705. this—passing <code>"start"</code> or <code>"end"</code> will
  706. collapse the selection to the start or end of the inserted
  707. text.</dd>
  708. <dt id="getCursor"><code><strong>doc.getCursor</strong>(?start: string) → {line, ch}</code></dt>
  709. <dd><code>start</code> is a an optional string indicating which
  710. end of the selection to return. It may
  711. be <code>"start"</code>, <code>"end"</code>, <code>"head"</code>
  712. (the side of the selection that moves when you press
  713. shift+arrow), or <code>"anchor"</code> (the fixed side of the
  714. selection). Omitting the argument is the same as
  715. passing <code>"head"</code>. A <code>{line, ch}</code> object
  716. will be returned.</dd>
  717. <dt id="somethingSelected"><code><strong>doc.somethingSelected</strong>() → boolean</code></dt>
  718. <dd>Return true if any text is selected.</dd>
  719. <dt id="setCursor"><code><strong>doc.setCursor</strong>(pos: {line, ch})</code></dt>
  720. <dd>Set the cursor position. You can either pass a
  721. single <code>{line, ch}</code> object, or the line and the
  722. character as two separate parameters.</dd>
  723. <dt id="setSelection"><code><strong>doc.setSelection</strong>(anchor: {line, ch}, head: {line, ch})</code></dt>
  724. <dd>Set the selection range. <code>anchor</code>
  725. and <code>head</code> should be <code>{line, ch}</code>
  726. objects. <code>head</code> defaults to <code>anchor</code> when
  727. not given.</dd>
  728. <dt id="extendSelection"><code><strong>doc.extendSelection</strong>(from: {line, ch}, ?to: {line, ch})</code></dt>
  729. <dd>Similar
  730. to <a href="#setSelection"><code>setSelection</code></a>, but
  731. will, if shift is held or
  732. the <a href="#setExtending">extending</a> flag is set, move the
  733. head of the selection while leaving the anchor at its current
  734. place. <code>pos2</code> is optional, and can be passed to
  735. ensure a region (for example a word or paragraph) will end up
  736. selected (in addition to whatever lies between that region and
  737. the current anchor).</dd>
  738. <dt id="setExtending"><code><strong>doc.setExtending</strong>(value: boolean)</code></dt>
  739. <dd>Sets or clears the 'extending' flag, which acts similar to
  740. the shift key, in that it will cause cursor movement and calls
  741. to <a href="#extendSelection"><code>extendSelection</code></a>
  742. to leave the selection anchor in place.</dd>
  743. <dt id="hasFocus"><code><strong>cm.hasFocus</strong>() → boolean</code></dt>
  744. <dd>Tells you whether the editor currently has focus.</dd>
  745. <dt id="findPosH"><code><strong>cm.findPosH</strong>(start: {line, ch}, amount: integer, unit: string, visually: boolean) → {line, ch, ?hitSide: boolean}</code></dt>
  746. <dd>Used to find the target position for horizontal cursor
  747. motion. <code>start</code> is a <code>{line, ch}</code>
  748. object, <code>amount</code> an integer (may be negative),
  749. and <code>unit</code> one of the
  750. string <code>"char"</code>, <code>"column"</code>,
  751. or <code>"word"</code>. Will return a position that is produced
  752. by moving <code>amount</code> times the distance specified
  753. by <code>unit</code>. When <code>visually</code> is true, motion
  754. in right-to-left text will be visual rather than logical. When
  755. the motion was clipped by hitting the end or start of the
  756. document, the returned value will have a <code>hitSide</code>
  757. property set to true.</dd>
  758. <dt id="findPosV"><code><strong>cm.findPosV</strong>(start: {line, ch}, amount: integer, unit: string) → {line, ch, ?hitSide: boolean}</code></dt>
  759. <dd>Similar to <a href="#findPosH"><code>findPosH</code></a>,
  760. but used for vertical motion. <code>unit</code> may
  761. be <code>"line"</code> or <code>"page"</code>. The other
  762. arguments and the returned value have the same interpretation as
  763. they have in <code>findPosH</code>.</dd>
  764. </dl>
  765. <h3 id="api_configuration">Configuration methods</h3>
  766. <dl>
  767. <dt id="setOption"><code><strong>cm.setOption</strong>(option: string, value: any)</code></dt>
  768. <dd>Change the configuration of the editor. <code>option</code>
  769. should the name of an <a href="#config">option</a>,
  770. and <code>value</code> should be a valid value for that
  771. option.</dd>
  772. <dt id="getOption"><code><strong>cm.getOption</strong>(option: string) → any</code></dt>
  773. <dd>Retrieves the current value of the given option for this
  774. editor instance.</dd>
  775. <dt id="addKeyMap"><code><strong>cm.addKeyMap</strong>(map: object, bottom: boolean)</code></dt>
  776. <dd>Attach an additional <a href="#keymaps">keymap</a> to the
  777. editor. This is mostly useful for add-ons that need to register
  778. some key handlers without trampling on
  779. the <a href="#option_extraKeys"><code>extraKeys</code></a>
  780. option. Maps added in this way have a higher precedence than
  781. the <code>extraKeys</code>
  782. and <a href="#option_keyMap"><code>keyMap</code></a> options,
  783. and between them, the maps added earlier have a lower precedence
  784. than those added later, unless the <code>bottom</code> argument
  785. was passed, in which case they end up below other keymaps added
  786. with this method.</dd>
  787. <dt id="removeKeyMap"><code><strong>cm.removeKeyMap</strong>(map: object)</code></dt>
  788. <dd>Disable a keymap added
  789. with <a href="#addKeyMap"><code>addKeyMap</code></a>. Either
  790. pass in the keymap object itself, or a string, which will be
  791. compared against the <code>name</code> property of the active
  792. keymaps.</dd>
  793. <dt id="addOverlay"><code><strong>cm.addOverlay</strong>(mode: string|object, ?options: object)</code></dt>
  794. <dd>Enable a highlighting overlay. This is a stateless mini-mode
  795. that can be used to add extra highlighting. For example,
  796. the <a href="../demo/search.html">search add-on</a> uses it to
  797. highlight the term that's currently being
  798. searched. <code>mode</code> can be a <a href="#option_mode">mode
  799. spec</a> or a mode object (an object with
  800. a <a href="#token"><code>token</code></a> method).
  801. The <code>options</code> parameter is optional. If given, it
  802. should be an object. Currently, only the <code>opaque</code>
  803. option is recognized. This defaults to off, but can be given to
  804. allow the overlay styling, when not <code>null</code>, to
  805. override the styling of the base mode entirely, instead of the
  806. two being applied together.</dd>
  807. <dt id="removeOverlay"><code><strong>cm.removeOverlay</strong>(mode: string|object)</code></dt>
  808. <dd>Pass this the exact argument passed for
  809. the <code>mode</code> parameter
  810. to <a href="#addOverlay"><code>addOverlay</code></a> to remove
  811. an overlay again.</dd>
  812. <dt id="on"><code><strong>cm.on</strong>(type: string, func: (...args))</code></dt>
  813. <dd>Register an event handler for the given event type (a
  814. string) on the editor instance. There is also
  815. a <code>CodeMirror.on(object, type, func)</code> version
  816. that allows registering of events on any object.</dd>
  817. <dt id="off"><code><strong>cm.off</strong>(type: string, func: (...args))</code></dt>
  818. <dd>Remove an event handler on the editor instance. An
  819. equivalent <code>CodeMirror.off(object, type,
  820. func)</code> also exists.</dd>
  821. </dl>
  822. <h3 id="api_doc">Document management methods</h3>
  823. <p id="Doc">Each editor is associated with an instance
  824. of <code>CodeMirror.Doc</code>, its document. A document
  825. represents the editor content, plus a selection, an undo history,
  826. and a <a href="#option_mode">mode</a>. A document can only be
  827. associated with a single editor at a time. You can create new
  828. documents by calling the <code>CodeMirror.Doc(text, mode,
  829. firstLineNumber)</code> constructor. The last two arguments are
  830. optional and can be used to set a mode for the document and make
  831. it start at a line number other than 0, respectively.</p>
  832. <dl>
  833. <dt id="getDoc"><code><strong>cm.getDoc</strong>() → Doc</code></dt>
  834. <dd>Retrieve the currently active document from an editor.</dd>
  835. <dt id="getEditor"><code><strong>doc.getEditor</strong>() → CodeMirror</code></dt>
  836. <dd>Retrieve the editor associated with a document. May
  837. return <code>null</code>.</dd>
  838. <dt id="swapDoc"><code><strong>cm.swapDoc</strong>(doc: CodeMirror.Doc) → Doc</code></dt>
  839. <dd>Attach a new document to the editor. Returns the old
  840. document, which is now no longer associated with an editor.</dd>
  841. <dt id="copy"><code><strong>doc.copy</strong>(copyHistory: boolean) → Doc</code></dt>
  842. <dd>Create an identical copy of the given doc.
  843. When <code>copyHistory</code> is true, the history will also be
  844. copied. Can not be called directly on an editor.</dd>
  845. <dt id="linkedDoc"><code><strong>doc.linkedDoc</strong>(options: object) → Doc</code></dt>
  846. <dd>Create a new document that's linked to the target document.
  847. Linked documents will stay in sync (changes to one are also
  848. applied to the other) until <a href="#unlinkDoc">unlinked</a>.
  849. These are the options that are supported:
  850. <dl>
  851. <dt id="linkedDoc_sharedHist"><code><strong>sharedHist</strong>: boolean</code></dt>
  852. <dd>When turned on, the linked copy will share an undo
  853. history with the original. Thus, something done in one of
  854. the two can be undone in the other, and vice versa.</dd>
  855. <dt id="linkedDoc_from"><code><strong>from</strong>: integer</code></dt>
  856. <dt id="linkedDoc_to"><code><strong>to</strong>: integer</code></dt>
  857. <dd>Can be given to make the new document a subview of the
  858. original. Subviews only show a given range of lines. Note
  859. that line coordinates inside the subview will be consistent
  860. with those of the parent, so that for example a subview
  861. starting at line 10 will refer to its first line as line 10,
  862. not 0.</dd>
  863. <dt id="linkedDoc_mode"><code><strong>mode</strong>: string|object</code></dt>
  864. <dd>By default, the new document inherits the mode of the
  865. parent. This option can be set to
  866. a <a href="#option_mode">mode spec</a> to give it a
  867. different mode.</dd>
  868. </dl></dd>
  869. <dt id="unlinkDoc"><code><strong>doc.unlinkDoc</strong>(doc: CodeMirror.Doc)</code></dt>
  870. <dd>Break the link between two documents. After calling this,
  871. changes will no longer propagate between the documents, and, if
  872. they had a shared history, the history will become
  873. separate.</dd>
  874. <dt id="iterLinkedDocs"><code><strong>doc.iterLinkedDocs</strong>(function: (doc: CodeMirror.Doc, sharedHist: boolean))</code></dt>
  875. <dd>Will call the given function for all documents linked to the
  876. target document. It will be passed two arguments, the linked document
  877. and a boolean indicating whether that document shares history
  878. with the target.</dd>
  879. </dl>
  880. <h3 id="api_history">History-related methods</h3>
  881. <dl>
  882. <dt id="undo"><code><strong>doc.undo</strong>()</code></dt>
  883. <dd>Undo one edit (if any undo events are stored).</dd>
  884. <dt id="redo"><code><strong>doc.redo</strong>()</code></dt>
  885. <dd>Redo one undone edit.</dd>
  886. <dt id="historySize"><code><strong>doc.historySize</strong>() → {undo: integer, redo: integer}</code></dt>
  887. <dd>Returns an object with <code>{undo, redo}</code> properties,
  888. both of which hold integers, indicating the amount of stored
  889. undo and redo operations.</dd>
  890. <dt id="clearHistory"><code><strong>doc.clearHistory</strong>()</code></dt>
  891. <dd>Clears the editor's undo history.</dd>
  892. <dt id="getHistory"><code><strong>doc.getHistory</strong>() → object</code></dt>
  893. <dd>Get a (JSON-serializeable) representation of the undo history.</dd>
  894. <dt id="setHistory"><code><strong>doc.setHistory</strong>(history: object)</code></dt>
  895. <dd>Replace the editor's undo history with the one provided,
  896. which must be a value as returned
  897. by <a href="#getHistory"><code>getHistory</code></a>. Note that
  898. this will have entirely undefined results if the editor content
  899. isn't also the same as it was when <code>getHistory</code> was
  900. called.</dd>
  901. </dl>
  902. <h3 id="api_marker">Text-marking methods</h3>
  903. <dl>
  904. <dt id="markText"><code><strong>doc.markText</strong>(from: {line, ch}, to: {line, ch}, ?options: object) → TextMarker</code></dt>
  905. <dd>Can be used to mark a range of text with a specific CSS
  906. class name. <code>from</code> and <code>to</code> should
  907. be <code>{line, ch}</code> objects. The <code>options</code>
  908. parameter is optional. When given, it should be an object that
  909. may contain the following configuration options:
  910. <dl>
  911. <dt id="mark_className"><code><strong>className</strong>: string</code></dt>
  912. <dd>Assigns a CSS class to the marked stretch of text.</dd>
  913. <dt id="mark_inclusiveLeft"><code><strong>inclusiveLeft</strong>: boolean</code></dt>
  914. <dd>Determines whether
  915. text inserted on the left of the marker will end up inside
  916. or outside of it.</dd>
  917. <dt id="mark_inclusiveRight"><code><strong>inclusiveRight</strong>: boolean</code></dt>
  918. <dd>Like <code>inclusiveLeft</code>,
  919. but for the right side.</dd>
  920. <dt id="mark_atomic"><code><strong>atomic</strong>: boolean</code></dt>
  921. <dd>Atomic ranges act as a single unit when cursor movement is
  922. concerned—i.e. it is impossible to place the cursor inside of
  923. them. In atomic ranges, <code>inclusiveLeft</code>
  924. and <code>inclusiveRight</code> have a different meaning—they
  925. will prevent the cursor from being placed respectively
  926. directly before and directly after the range.</dd>
  927. <dt id="mark_collapsed"><code><strong>collapsed</strong>: boolean</code></dt>
  928. <dd>Collapsed ranges do not show up in the display. Setting a
  929. range to be collapsed will automatically make it atomic.</dd>
  930. <dt id="mark_clearOnEnter"><code><strong>clearOnEnter</strong>: boolean</code></dt>
  931. <dd>When enabled, will cause the mark to clear itself whenever
  932. the cursor enters its range. This is mostly useful for
  933. text-replacement widgets that need to 'snap open' when the
  934. user tries to edit them. The
  935. <a href="#event_clear"><code>"clear"</code></a> event
  936. fired on the range handle can be used to be notified when this
  937. happens.</dd>
  938. <dt id="mark_replacedWith"><code><strong>replacedWith</strong>: Element</code></dt>
  939. <dd>Use a given node to display this range. Implies both
  940. collapsed and atomic. The given DOM node <em>must</em> be an
  941. inline element (as opposed to a block element).</dd>
  942. <dt id="mark_readOnly"><code><strong>readOnly</strong>: boolean</code></dt>
  943. <dd>A read-only span can, as long as it is not cleared, not be
  944. modified except by
  945. calling <a href="#setValue"><code>setValue</code></a> to reset
  946. the whole document. <em>Note:</em> adding a read-only span
  947. currently clears the undo history of the editor, because
  948. existing undo events being partially nullified by read-only
  949. spans would corrupt the history (in the current
  950. implementation).</dd>
  951. <dt id="mark_addToHistory"><code><strong>addToHistory</strong>: boolean</code></dt>
  952. <dd>When set to true (default is false), adding this marker
  953. will create an event in the undo history that can be
  954. individually undone (clearing the marker).</dd>
  955. <dt id="mark_startStyle"><code><strong>startStyle</strong>: string</code></dt><dd>Can be used to specify
  956. an extra CSS class to be applied to the leftmost span that
  957. is part of the marker.</dd>
  958. <dt id="mark_endStyle"><code><strong>endStyle</strong>: string</code></dt><dd>Equivalent
  959. to <code>startStyle</code>, but for the rightmost span.</dd>
  960. <dt id="mark_shared"><code><strong>shared</strong>: boolean</code></dt><dd>When the
  961. target document is <a href="#linkedDoc">linked</a> to other
  962. documents, you can set <code>shared</code> to true to make the
  963. marker appear in all documents. By default, a marker appears
  964. only in its target document.</dd>
  965. </dl>
  966. The method will return an object that represents the marker
  967. (with constuctor <code>CodeMirror.TextMarker</code>), which
  968. exposes three methods:
  969. <code><strong>clear</strong>()</code>, to remove the mark,
  970. <code><strong>find</strong>()</code>, which returns
  971. a <code>{from, to}</code> object (both holding document
  972. positions), indicating the current position of the marked range,
  973. or <code>undefined</code> if the marker is no longer in the
  974. document, and finally <code><strong>changed</strong>()</code>,
  975. which you can call if you've done something that might change
  976. the size of the marker (for example changing the content of
  977. a <a href="#mark_replacedWith"><code>replacedWith</code></a>
  978. node), and want to cheaply update the display.</dd>
  979. <dt id="setBookmark"><code><strong>doc.setBookmark</strong>(pos: {line, ch}, ?options: object) → TextMarker</code></dt>
  980. <dd>Inserts a bookmark, a handle that follows the text around it
  981. as it is being edited, at the given position. A bookmark has two
  982. methods <code>find()</code> and <code>clear()</code>. The first
  983. returns the current position of the bookmark, if it is still in
  984. the document, and the second explicitly removes the bookmark.
  985. The options argument is optional. If given, the following
  986. properties are recognized:
  987. <dl>
  988. <dt><code><strong>widget</strong>: Element</code></dt><dd>Can be used to display a DOM
  989. node at the current location of the bookmark (analogous to
  990. the <a href="#mark_replacedWith"><code>replacedWith</code></a>
  991. option to <code>markText</code>).</dd>
  992. <dt><code><strong>insertLeft</strong>: boolean</code></dt><dd>By default, text typed
  993. when the cursor is on top of the bookmark will end up to the
  994. right of the bookmark. Set this option to true to make it go
  995. to the left instead.</dd>
  996. </dl></dd>
  997. <dt id="findMarksAt"><code><strong>doc.findMarksAt</strong>(pos: {line, ch}) → array&lt;TextMarker&gt;</code></dt>
  998. <dd>Returns an array of all the bookmarks and marked ranges
  999. present at the given position.</dd>
  1000. <dt id="getAllMarks"><code><strong>doc.getAllMarks</strong>() → array&lt;TextMarker&gt;</code></dt>
  1001. <dd>Returns an array containing all marked ranges in the document.</dd>
  1002. </dl>
  1003. <h3 id="api_decoration">Widget, gutter, and decoration methods</h3>
  1004. <dl>
  1005. <dt id="setGutterMarker"><code><strong>cm.setGutterMarker</strong>(line: integer|LineHandle, gutterID: string, value: Element) → LineHandle</code></dt>
  1006. <dd>Sets the gutter marker for the given gutter (identified by
  1007. its CSS class, see
  1008. the <a href="#option_gutters"><code>gutters</code></a> option)
  1009. to the given value. Value can be either <code>null</code>, to
  1010. clear the marker, or a DOM element, to set it. The DOM element
  1011. will be shown in the specified gutter next to the specified
  1012. line.</dd>
  1013. <dt id="clearGutter"><code><strong>cm.clearGutter</strong>(gutterID: string)</code></dt>
  1014. <dd>Remove all gutter markers in
  1015. the <a href="#option_gutters">gutter</a> with the given ID.</dd>
  1016. <dt id="addLineClass"><code><strong>cm.addLineClass</strong>(line: integer|LineHandle, where: string, class: string) → LineHandle</code></dt>
  1017. <dd>Set a CSS class name for the given line. <code>line</code>
  1018. can be a number or a line handle. <code>where</code> determines
  1019. to which element this class should be applied, can can be one
  1020. of <code>"text"</code> (the text element, which lies in front of
  1021. the selection), <code>"background"</code> (a background element
  1022. that will be behind the selection), or <code>"wrap"</code> (the
  1023. wrapper node that wraps all of the line's elements, including
  1024. gutter elements). <code>class</code> should be the name of the
  1025. class to apply.</dd>
  1026. <dt id="removeLineClass"><code><strong>cm.removeLineClass</strong>(line: integer|LineHandle, where: string, class: string) → LineHandle</code></dt>
  1027. <dd>Remove a CSS class from a line. <code>line</code> can be a
  1028. line handle or number. <code>where</code> should be one
  1029. of <code>"text"</code>, <code>"background"</code>,
  1030. or <code>"wrap"</code>
  1031. (see <a href="#addLineClass"><code>addLineClass</code></a>). <code>class</code>
  1032. can be left off to remove all classes for the specified node, or
  1033. be a string to remove only a specific class.</dd>
  1034. <dt id="lineInfo"><code><strong>cm.lineInfo</strong>(line: integer|LineHandle) → object</code></dt>
  1035. <dd>Returns the line number, text content, and marker status of
  1036. the given line, which can be either a number or a line handle.
  1037. The returned object has the structure <code>{line, handle, text,
  1038. gutterMarkers, textClass, bgClass, wrapClass, widgets}</code>,
  1039. where <code>gutterMarkers</code> is an object mapping gutter IDs
  1040. to marker elements, and <code>widgets</code> is an array
  1041. of <a href="#addLineWidget">line widgets</a> attached to this
  1042. line, and the various class properties refer to classes added
  1043. with <a href="#addLineClass"><code>addLineClass</code></a>.</dd>
  1044. <dt id="addWidget"><code><strong>cm.addWidget</strong>(pos: {line, ch}, node: Element, scrollIntoView: boolean)</code></dt>
  1045. <dd>Puts <code>node</code>, which should be an absolutely
  1046. positioned DOM node, into the editor, positioned right below the
  1047. given <code>{line, ch}</code> position.
  1048. When <code>scrollIntoView</code> is true, the editor will ensure
  1049. that the entire node is visible (if possible). To remove the
  1050. widget again, simply use DOM methods (move it somewhere else, or
  1051. call <code>removeChild</code> on its parent).</dd>
  1052. <dt id="addLineWidget"><code><strong>cm.addLineWidget</strong>(line: integer|LineHandle, node: Element, ?options: object) → LineWidget</code></dt>
  1053. <dd>Adds a line widget, an element shown below a line, spanning
  1054. the whole of the editor's width, and moving the lines below it
  1055. downwards. <code>line</code> should be either an integer or a
  1056. line handle, and <code>node</code> should be a DOM node, which
  1057. will be displayed below the given line. <code>options</code>,
  1058. when given, should be an object that configures the behavior of
  1059. the widget. The following options are supported (all default to
  1060. false) →
  1061. <dl>
  1062. <dt><code><strong>coverGutter</strong>: boolean</code></dt>
  1063. <dd>Whether the widget should cover the gutter.</dd>
  1064. <dt><code><strong>noHScroll</strong>: boolean</code></dt>
  1065. <dd>Whether the widget should stay fixed in the face of
  1066. horizontal scrolling.</dd>
  1067. <dt><code><strong>above</strong>: boolean</code></dt>
  1068. <dd>Causes the widget to be placed above instead of below
  1069. the text of the line.</dd>
  1070. <dt><code><strong>showIfHidden</strong>: boolean</code></dt>
  1071. <dd>When true, will cause the widget to be rendered even if
  1072. the line it is associated with is hidden.</dd>
  1073. </dl>
  1074. Note that the widget node will become a descendant of nodes with
  1075. CodeMirror-specific CSS classes, and those classes might in some
  1076. cases affect it. This method returns an object that represents
  1077. the widget placement. It'll have a <code>line</code> property
  1078. pointing at the line handle that it is associated with, and the following methods:
  1079. <dl>
  1080. <dt id="widget_clear"><code><strong>clear</strong>()</code></dt><dd>Removes the widget.</dd>
  1081. <dt id="widget_changed"><code><strong>changed</strong>()</code></dt><dd>Call
  1082. this if you made some change to the widget's DOM node that
  1083. might affect its height. It'll force CodeMirror to update
  1084. the height of the line that contains the widget.</dd>
  1085. </dl>
  1086. </dd>
  1087. </dl>
  1088. <h3 id="api_sizing">Sizing, scrolling and positioning methods</h3>
  1089. <dl>
  1090. <dt id="setSize"><code><strong>cm.setSize</strong>(width: number|string, height: number|string)</code></dt>
  1091. <dd>Programatically set the size of the editor (overriding the
  1092. applicable <a href="#css-resize">CSS
  1093. rules</a>). <code>width</code> and <code>height</code> height
  1094. can be either numbers (interpreted as pixels) or CSS units
  1095. (<code>"100%"</code>, for example). You can
  1096. pass <code>null</code> for either of them to indicate that that
  1097. dimension should not be changed.</dd>
  1098. <dt id="scrollTo"><code><strong>cm.scrollTo</strong>(x: number, y: number)</code></dt>
  1099. <dd>Scroll the editor to a given (pixel) position. Both
  1100. arguments may be left as <code>null</code>
  1101. or <code>undefined</code> to have no effect.</dd>
  1102. <dt id="getScrollInfo"><code><strong>cm.getScrollInfo</strong>() → {left, top, width, height, clientWidth, clientHeight}</code></dt>
  1103. <dd>Get an <code>{left, top, width, height, clientWidth,
  1104. clientHeight}</code> object that represents the current scroll
  1105. position, the size of the scrollable area, and the size of the
  1106. visible area (minus scrollbars).</dd>
  1107. <dt id="scrollIntoView"><code><strong>cm.scrollIntoView</strong>(pos: {line, ch}|{left, top, right, bottom}, ?margin: number)</code></dt>
  1108. <dd>Scrolls the given element into view. <code>pos</code> may be
  1109. either a <code>{line, ch}</code> position, referring to a given
  1110. character, <code>null</code>, to refer to the cursor, or
  1111. a <code>{left, top, right, bottom}</code> object, in
  1112. editor-local coordinates. The <code>margin</code> parameter is
  1113. optional. When given, it indicates the amount of pixels around
  1114. the given area that should be made visible as well.</dd>
  1115. <dt id="cursorCoords"><code><strong>cm.cursorCoords</strong>(where: boolean|{line, ch}, mode: string) → {left, top, bottom}</code></dt>
  1116. <dd>Returns an <code>{left, top, bottom}</code> object
  1117. containing the coordinates of the cursor position.
  1118. If <code>mode</code> is <code>"local"</code>, they will be
  1119. relative to the top-left corner of the editable document. If it
  1120. is <code>"page"</code> or not given, they are relative to the
  1121. top-left corner of the page. <code>where</code> can be a boolean
  1122. indicating whether you want the start (<code>true</code>) or the
  1123. end (<code>false</code>) of the selection, or, if a <code>{line,
  1124. ch}</code> object is given, it specifies the precise position at
  1125. which you want to measure.</dd>
  1126. <dt id="charCoords"><code><strong>cm.charCoords</strong>(pos: {line, ch}, mode: string) → {left, right, top, bottom}</code></dt>
  1127. <dd>Returns the position and dimensions of an arbitrary
  1128. character. <code>pos</code> should be a <code>{line, ch}</code>
  1129. object. This differs from <code>cursorCoords</code> in that
  1130. it'll give the size of the whole character, rather than just the
  1131. position that the cursor would have when it would sit at that
  1132. position.</dd>
  1133. <dt id="coordsChar"><code><strong>cm.coordsChar</strong>(object: {left, top}, ?mode: string) → {line, ch}</code></dt>
  1134. <dd>Given an <code>{left, top}</code> object, returns
  1135. the <code>{line, ch}</code> position that corresponds to it. The
  1136. optional <code>mode</code> parameter determines relative to what
  1137. the coordinates are interpreted. It may
  1138. be <code>"window"</code>, <code>"page"</code> (the default),
  1139. or <code>"local"</code>.</dd>
  1140. <dt id="defaultTextHeight"><code><strong>cm.defaultTextHeight</strong>() → number</code></dt>
  1141. <dd>Returns the line height of the default font for the editor.</dd>
  1142. <dt id="defaultCharWidth"><code><strong>cm.defaultCharWidth</strong>() → number</code></dt>
  1143. <dd>Returns the pixel width of an 'x' in the default font for
  1144. the editor. (Note that for non-monospace fonts, this is mostly
  1145. useless, and even for monospace fonts, non-ascii characters
  1146. might have a different width).</dd>
  1147. <dt id="getViewport"><code><strong>cm.getViewport</strong>() → {from: number, to: number}</code></dt>
  1148. <dd>Returns a <code>{from, to}</code> object indicating the
  1149. start (inclusive) and end (exclusive) of the currently rendered
  1150. part of the document. In big documents, when most content is
  1151. scrolled out of view, CodeMirror will only render the visible
  1152. part, and a margin around it. See also
  1153. the <a href="#event_viewportChange"><code>viewportChange</code></a>
  1154. event.</dd>
  1155. <dt id="refresh"><code><strong>cm.refresh</strong>()</code></dt>
  1156. <dd>If your code does something to change the size of the editor
  1157. element (window resizes are already listened for), or unhides
  1158. it, you should probably follow up by calling this method to
  1159. ensure CodeMirror is still looking as intended.</dd>
  1160. </dl>
  1161. <h3 id="api_mode">Mode, state, and token-related methods</h3>
  1162. <p>When writing language-aware functionality, it can often be
  1163. useful to hook into the knowledge that the CodeMirror language
  1164. mode has. See <a href="#modeapi">the section on modes</a> for a
  1165. more detailed description of how these work.</p>
  1166. <dl>
  1167. <dt id="getMode"><code><strong>doc.getMode</strong>() → object</code></dt>
  1168. <dd>Gets the mode object for the editor. Note that this is
  1169. distinct from <code>getOption("mode")</code>, which gives you
  1170. the mode specification, rather than the resolved, instantiated
  1171. <a href="#defineMode">mode object</a>.</dd>
  1172. <dt id="getTokenAt"><code><strong>cm.getTokenAt</strong>(pos: {line, ch}) → object</code></dt>
  1173. <dd>Retrieves information about the token the current mode found
  1174. before the given position (a <code>{line, ch}</code> object). The
  1175. returned object has the following properties:
  1176. <dl>
  1177. <dt><code><strong>start</strong></code></dt><dd>The character (on the given line) at which the token starts.</dd>
  1178. <dt><code><strong>end</strong></code></dt><dd>The character at which the token ends.</dd>
  1179. <dt><code><strong>string</strong></code></dt><dd>The token's string.</dd>
  1180. <dt><code><strong>type</strong></code></dt><dd>The token type the mode assigned
  1181. to the token, such as <code>"keyword"</code>
  1182. or <code>"comment"</code> (may also be null).</dd>
  1183. <dt><code><strong>state</strong></code></dt><dd>The mode's state at the end of this token.</dd>
  1184. </dl></dd>
  1185. <dt id="getStateAfter"><code><strong>cm.getStateAfter</strong>(?line: integer) → object</code></dt>
  1186. <dd>Returns the mode's parser state, if any, at the end of the
  1187. given line number. If no line number is given, the state at the
  1188. end of the document is returned. This can be useful for storing
  1189. parsing errors in the state, or getting other kinds of
  1190. contextual information for a line.</dd>
  1191. </dl>
  1192. <h3 id="api_misc">Miscellaneous methods</h3>
  1193. <dl>
  1194. <dt id="operation"><code><strong>cm.operation</strong>(func: () → any) → any</code></dt>
  1195. <dd>CodeMirror internally buffers changes and only updates its
  1196. DOM structure after it has finished performing some operation.
  1197. If you need to perform a lot of operations on a CodeMirror
  1198. instance, you can call this method with a function argument. It
  1199. will call the function, buffering up all changes, and only doing
  1200. the expensive update after the function returns. This can be a
  1201. lot faster. The return value from this method will be the return
  1202. value of your function.</dd>
  1203. <dt id="indentLine"><code><strong>cm.indentLine</strong>(line: integer, ?dir: string)</code></dt>
  1204. <dd>Adjust the indentation of the given line. The second
  1205. argument (which defaults to <code>"smart"</code>) may be one of:
  1206. <dl>
  1207. <dt><code><strong>"prev"</strong></code></dt>
  1208. <dd>Base indentation on the indentation of the previous line.</dd>
  1209. <dt><code><strong>"smart"</strong></code></dt>
  1210. <dd>Use the mode's smart indentation if available, behave
  1211. like <code>"prev"</code> otherwise.</dd>
  1212. <dt><code><strong>"add"</strong></code></dt>
  1213. <dd>Increase the indentation of the line by
  1214. one <a href="#option_indentUnit">indent unit</a>.</dd>
  1215. <dt><code><strong>"subtract"</strong></code></dt>
  1216. <dd>Reduce the indentation of the line.</dd>
  1217. </dl></dd>
  1218. <dt id="toggleOverwrite"><code><strong>cm.toggleOverwrite</strong>(?value: bool)</code></dt>
  1219. <dd>Switches between overwrite and normal insert mode (when not
  1220. given an argument), or sets the overwrite mode to a specific
  1221. state (when given an argument).</dd>
  1222. <dt id="posFromIndex"><code><strong>doc.posFromIndex</strong>(index: integer) → {line, ch}</code></dt>
  1223. <dd>Calculates and returns a <code>{line, ch}</code> object for a
  1224. zero-based <code>index</code> who's value is relative to the start of the
  1225. editor's text. If the <code>index</code> is out of range of the text then
  1226. the returned object is clipped to start or end of the text
  1227. respectively.</dd>
  1228. <dt id="indexFromPos"><code><strong>doc.indexFromPos</strong>(object: {line, ch}) → integer</code></dt>
  1229. <dd>The reverse of <a href="#posFromIndex"><code>posFromIndex</code></a>.</dd>
  1230. <dt id="focus"><code><strong>cm.focus</strong>()</code></dt>
  1231. <dd>Give the editor focus.</dd>
  1232. <dt id="getInputField"><code><strong>cm.getInputField</strong>() → TextAreaElement</code></dt>
  1233. <dd>Returns the hidden textarea used to read input.</dd>
  1234. <dt id="getWrapperElement"><code><strong>cm.getWrapperElement</strong>() → Element</code></dt>
  1235. <dd>Returns the DOM node that represents the editor, and
  1236. controls its size. Remove this from your tree to delete an
  1237. editor instance.</dd>
  1238. <dt id="getScrollerElement"><code><strong>cm.getScrollerElement</strong>() → Element</code></dt>
  1239. <dd>Returns the DOM node that is responsible for the scrolling
  1240. of the editor.</dd>
  1241. <dt id="getGutterElement"><code><strong>cm.getGutterElement</strong>() → Element</code></dt>
  1242. <dd>Fetches the DOM node that contains the editor gutters.</dd>
  1243. </dl>
  1244. <h3 id="api_static">Static properties</h3>
  1245. <p>The <code>CodeMirror</code> object itself provides
  1246. several useful properties.</p>
  1247. <dl>
  1248. <dt id="version"><code><strong>CodeMirror.version</strong>: string</code></dt>
  1249. <dd>It contains a string that indicates the version of the
  1250. library. For releases, this simply
  1251. contains <code>"major.minor"</code> (for
  1252. example <code>"2.33"</code>. For beta versions, <code>" B"</code>
  1253. (space, capital B) is added at the end of the string, for
  1254. development snapshots, <code>" +"</code> (space, plus) is
  1255. added.</dd>
  1256. <dt id="fromTextArea"><code><strong>CodeMirror.fromTextArea</strong>(textArea: TextAreaElement, ?config: object)</code></dt>
  1257. <dd>
  1258. The method provides another way to initialize an editor. It takes a
  1259. textarea DOM node as first argument and an optional configuration
  1260. object as second. It will replace the textarea with a CodeMirror
  1261. instance, and wire up the form of that textarea (if any) to make
  1262. sure the editor contents are put into the textarea when the form
  1263. is submitted. A CodeMirror instance created this way has three
  1264. additional methods:
  1265. <dl>
  1266. <dt id="save"><code><strong>cm.save</strong>()</code></dt>
  1267. <dd>Copy the content of the editor into the textarea.</dd>
  1268. <dt id="toTextArea"><code><strong>cm.toTextArea</strong>()</code></dt>
  1269. <dd>Remove the editor, and restore the original textarea (with
  1270. the editor's current content).</dd>
  1271. <dt id="getTextArea"><code><strong>cm.getTextArea</strong>() → TextAreaElement</code></dt>
  1272. <dd>Returns the textarea that the instance was based on.</dd>
  1273. </dl>
  1274. </dd>
  1275. <dt id="defaults"><code><strong>CodeMirror.defaults</strong>: object</code></dt>
  1276. <dd>An object containing default values for
  1277. all <a href="#config">options</a>. You can assign to its
  1278. properties to modify defaults (though this won't affect editors
  1279. that have already been created).</dd>
  1280. <dt id="defineExtension"><code><strong>CodeMirror.defineExtension</strong>(name: string, value: any)</code></dt>
  1281. <dd>If you want to define extra methods in terms of the
  1282. CodeMirror API, it is possible to
  1283. use <code>defineExtension</code>. This will cause the given
  1284. value (usually a method) to be added to all CodeMirror instances
  1285. created from then on.</dd>
  1286. <dt id="defineDocExtension"><code><strong>CodeMirror.defineDocExtension</strong>(name: string, value: any)</code></dt>
  1287. <dd>Like <a href="#defineExtenstion"><code>defineExtension</code></a>,
  1288. but the method will be added to the interface
  1289. for <a href="#Doc"><code>Doc</code></a> objects instead.</dd>
  1290. <dt id="defineOption"><code><strong>CodeMirror.defineOption</strong>(name: string,
  1291. default: any, updateFunc: function)</code></dt>
  1292. <dd>Similarly, <code>defineOption</code> can be used to define new options for
  1293. CodeMirror. The <code>updateFunc</code> will be called with the
  1294. editor instance and the new value when an editor is initialized,
  1295. and whenever the option is modified
  1296. through <a href="#setOption"><code>setOption</code></a>.</dd>
  1297. <dt id="defineInitHook"><code><strong>CodeMirror.defineInitHook</strong>(func: function)</code></dt>
  1298. <dd>If your extention just needs to run some
  1299. code whenever a CodeMirror instance is initialized,
  1300. use <code>CodeMirror.defineInitHook</code>. Give it a function as
  1301. its only argument, and from then on, that function will be called
  1302. (with the instance as argument) whenever a new CodeMirror instance
  1303. is initialized.</dd>
  1304. </dl>
  1305. <h2 id="addons">Add-ons</h2>
  1306. <p>The <code>addon</code> directory in the distribution contains a
  1307. number of reusable components that implement extra editor
  1308. functionality. In brief, they are:</p>
  1309. <dl>
  1310. <dt id="addon_dialog"><a href="../addon/dialog/dialog.js"><code>dialog/dialog.js</code></a></dt>
  1311. <dd>Provides a very simple way to query users for text input.
  1312. Adds an <code>openDialog</code> method to CodeMirror instances,
  1313. which can be called with an HTML fragment that provides the
  1314. prompt (should include an <code>input</code> tag), and a
  1315. callback function that is called when text has been entered.
  1316. Depends on <code>addon/dialog/dialog.css</code>.</dd>
  1317. <dt id="addon_searchcursor"><a href="../addon/search/searchcursor.js"><code>search/searchcursor.js</code></a></dt>
  1318. <dd>Adds the <code>getSearchCursor(query, start, caseFold) →
  1319. cursor</code> method to CodeMirror instances, which can be used
  1320. to implement search/replace functionality. <code>query</code>
  1321. can be a regular expression or a string (only strings will match
  1322. across lines—if they contain newlines). <code>start</code>
  1323. provides the starting position of the search. It can be
  1324. a <code>{line, ch}</code> object, or can be left off to default
  1325. to the start of the document. <code>caseFold</code> is only
  1326. relevant when matching a string. It will cause the search to be
  1327. case-insensitive. A search cursor has the following methods:
  1328. <dl>
  1329. <dt><code><strong>findNext</strong>() → boolean</code></dt>
  1330. <dt><code><strong>findPrevious</strong>() → boolean</code></dt>
  1331. <dd>Search forward or backward from the current position.
  1332. The return value indicates whether a match was found. If
  1333. matching a regular expression, the return value will be the
  1334. array returned by the <code>match</code> method, in case you
  1335. want to extract matched groups.</dd>
  1336. <dt><code><strong>from</strong>() → {line, ch}</code></dt>
  1337. <dt><code><strong>to</strong>() → {line, ch}</code></dt>
  1338. <dd>These are only valid when the last call
  1339. to <code>findNext</code> or <code>findPrevious</code> did
  1340. not return false. They will return <code>{line, ch}</code>
  1341. objects pointing at the start and end of the match.</dd>
  1342. <dt><code><strong>replace</strong>(text: string)</code></dt>
  1343. <dd>Replaces the currently found match with the given text
  1344. and adjusts the cursor position to reflect the
  1345. replacement.</dd>
  1346. </dl></dd>
  1347. <dt id="addon_search"><a href="../addon/search/search.js"><code>search/search.js</code></a></dt>
  1348. <dd>Implements the search commands. CodeMirror has keys bound to
  1349. these by default, but will not do anything with them unless an
  1350. implementation is provided. Depends
  1351. on <code>searchcursor.js</code>, and will make use
  1352. of <a href="#addon_dialog"><code>openDialog</code></a> when
  1353. available to make prompting for search queries less ugly.</dd>
  1354. <dt id="addon_matchbrackets"><a href="../addon/edit/matchbrackets.js"><code>edit/matchbrackets.js</code></a></dt>
  1355. <dd>Defines an option <code>matchBrackets</code> which, when set
  1356. to true, causes matching brackets to be highlighted whenever the
  1357. cursor is next to them. It also adds a
  1358. method <code>matchBrackets</code> that forces this to happen
  1359. once, and a method <code>findMatchingBracket</code> that can be
  1360. used to run the bracket-finding algorithm that this uses
  1361. internally.</dd>
  1362. <dt id="addon_closebrackets"><a href="../addon/edit/closebrackets.js"><code>edit/closebrackets.js</code></a></dt>
  1363. <dd>Defines an option <code>autoCloseBrackets</code> that will
  1364. auto-close brackets and quotes when typed. By default, it'll
  1365. auto-close <code>()[]{}''""</code>, but you can pass it a
  1366. string similar to that (containing pairs of matching characters)
  1367. to customize it. <a href="../demo/closebrackets.html">Demo
  1368. here</a>.</dd>
  1369. <dt id="addon_comment"><a href="../addon/comment/comment.js"><code>comment/comment.js</code></a></dt>
  1370. <dd>Addon for commenting and uncommenting code. Adds three
  1371. methods to CodeMirror instances:
  1372. <dl>
  1373. <dt id="lineComment"><code><strong>lineComment</strong>(from: {line, ch}, to: {line, ch}, ?options: object)</code></dt>
  1374. <dd>Set the lines in the given range to be line comments. Will
  1375. fall back to <code>blockComment</code> when no line comment
  1376. style is defined for the mode.</dd>
  1377. <dt id="blockComment"><code><strong>blockComment</strong>(from: {line, ch}, to: {line, ch}, ?options: object)</code></dt>
  1378. <dd>Wrap the code in the given range in a block comment. Will
  1379. fall back to <code>lineComment</code> when no block comment
  1380. style is defined for the mode.</dd>
  1381. <dt id="uncomment"><code><strong>uncomment</strong>(from: {line, ch}, to: {line, ch}, ?options: object) → boolean</code></dt>
  1382. <dd>Try to uncomment the given range.
  1383. Returns <code>true</code> if a comment range was found and
  1384. removed, <code>false</code> otherwise.</dd>
  1385. </dl>
  1386. The <code>options</code> object accepted by these methods may
  1387. have the following properties:
  1388. <dl>
  1389. <dt><code>blockCommentStart, blockCommentEnd, blockCommentLead, lineComment: string</code></dt>
  1390. <dd>Override the <a href="#mode_comment">comment string
  1391. properties</a> of the mode with custom comment strings.</dd>
  1392. <dt><code>padding</code></dt>
  1393. <dd>A string that will be inserted after opening and before
  1394. closing comment markers. Defaults to a single space.</dd>
  1395. <dt><code>commentBlankLines</code></dt>
  1396. <dd>Whether, when adding line comments, to also comment lines
  1397. that contain only whitespace.</dd>
  1398. <dt><code>indent</code></dt>
  1399. <dd>When adding line comments and this is turned on, it will
  1400. align the comment block to the current indentation of the
  1401. first line of the block.</dd>
  1402. <dt><code>fullLines</code></dt>
  1403. <dd>When block commenting, this controls whether the whole
  1404. lines are indented, or only the precise range that is given.
  1405. Defaults to <code>true</code>.</dd>
  1406. </dl>
  1407. The addon also defines
  1408. a <code>toggleComment</code> <a href="#commands">command</a>,
  1409. which will try to uncomment the current selection, and if that
  1410. fails, line-comments it.</dd>
  1411. <dt id="addon_foldcode"><a href="../addon/fold/foldcode.js"><code>fold/foldcode.js</code></a></dt>
  1412. <dd>Helps with code folding.
  1413. See <a href="../demo/folding.html">the demo</a> for an example.
  1414. Call <code>CodeMirror.newFoldFunction</code> with a range-finder
  1415. helper function to create a function that will, when applied to
  1416. a CodeMirror instance and a line number, attempt to fold or
  1417. unfold the block starting at the given line. A range-finder is a
  1418. language-specific function that also takes an instance and a
  1419. line number, and returns an range to be folded, or null if no
  1420. block is started on that line. There are files in
  1421. the <a href="../addon/fold/"><code>addon/fold/</code></a>
  1422. directory providing <code>CodeMirror.braceRangeFinder</code>,
  1423. which finds blocks in brace languages (JavaScript, C, Java,
  1424. etc), <code>CodeMirror.indentRangeFinder</code>, for languages
  1425. where indentation determines block structure (Python, Haskell),
  1426. and <code>CodeMirror.tagRangeFinder</code>, for XML-style
  1427. languages.</dd>
  1428. <dt id="addon_runmode"><a href="../addon/runmode/runmode.js"><code>runmode/runmode.js</code></a></dt>
  1429. <dd>Can be used to run a CodeMirror mode over text without
  1430. actually opening an editor instance.
  1431. See <a href="../demo/runmode.html">the demo</a> for an example.
  1432. There are alternate versions of the file avaible for
  1433. running <a href="../addon/runmode/runmode-standalone.js">stand-alone</a>
  1434. (without including all of CodeMirror) and
  1435. for <a href="../addon/runmode/runmode.node.js">running under
  1436. node.js</a>.</dd>
  1437. <dt id="addon_overlay"><a href="../addon/mode/overlay.js"><code>mode/overlay.js</code></a></dt>
  1438. <dd>Mode combinator that can be used to extend a mode with an
  1439. 'overlay' — a secondary mode is run over the stream, along with
  1440. the base mode, and can color specific pieces of text without
  1441. interfering with the base mode.
  1442. Defines <code>CodeMirror.overlayMode</code>, which is used to
  1443. create such a mode. See <a href="../demo/mustache.html">this
  1444. demo</a> for a detailed example.</dd>
  1445. <dt id="addon_multiplex"><a href="../addon/mode/multiplex.js"><code>mode/multiplex.js</code></a></dt>
  1446. <dd>Mode combinator that can be used to easily 'multiplex'
  1447. between several modes.
  1448. Defines <code>CodeMirror.multiplexingMode</code> which, when
  1449. given as first argument a mode object, and as other arguments
  1450. any number of <code>{open, close, mode [, delimStyle]}</code>
  1451. objects, will return a mode object that starts parsing using the
  1452. mode passed as first argument, but will switch to another mode
  1453. as soon as it encounters a string that occurs in one of
  1454. the <code>open</code> fields of the passed objects. When in a
  1455. sub-mode, it will go back to the top mode again when
  1456. the <code>close</code> string is encountered.
  1457. Pass <code>"\n"</code> for <code>open</code> or <code>close</code>
  1458. if you want to switch on a blank line.
  1459. When <code>delimStyle</code> is specified, it will be the token
  1460. style returned for the delimiter tokens. The outer mode will not
  1461. see the content between the delimiters.
  1462. See <a href="../demo/multiplex.html">this demo</a> for an
  1463. example.</dd>
  1464. <dt id="addon_show-hint"><a href="../addon/hint/show-hint.js"><code>hint/show-hint.js</code></a></dt>
  1465. <dd>Provides a framework for showing autocompletion hints.
  1466. Defines <code>CodeMirror.showHint</code>, which takes a
  1467. CodeMirror instance and a hinting function, and pops up a widget
  1468. that allows the user to select a completion. Hinting functions
  1469. are function that take an editor instance, and return
  1470. a <code>{list, from, to}</code> object, where <code>list</code>
  1471. is an array of strings (the completions), and <code>from</code>
  1472. and <code>to</code> give the start and end of the token that is
  1473. being completed. Depends
  1474. on <code>addon/hint/show-hint.css</code>. See the other files in
  1475. the <a href="../addon/hint/"><code>addon/hint</code></a> for
  1476. hint sources for various languages. Check
  1477. out <a href="../demo/complete.html">the demo</a> for an
  1478. example.</dd>
  1479. <dt id="addon_match-highlighter"><a href="../addon/search/match-highlighter.js"><code>match-highlighter.js</code></a></dt>
  1480. <dd>Adds a <code>highlightSelectionMatches</code> option that
  1481. can be enabled to highlight all instances of a currently
  1482. selected word.
  1483. Demo <a href="../demo/matchhighlighter.html">here</a>.</dd>
  1484. <dt id="addon_lint"><a href="../addon/lint/lint.js"><code>lint/lint.js</code></a></dt>
  1485. <dd>Defines an interface component for showing linting warnings,
  1486. with pluggable warning sources
  1487. (see <a href="../addon/lint/json-lint.js"><code>json-lint.js</code></a>
  1488. and <a href="../addon/lint/javascript-lint.js"><code>javascript-lint.js</code></a>
  1489. in the same directory). Defines a <code>lintWith</code> option
  1490. that can be set to a warning source (for
  1491. example <code>CodeMirror.javascriptValidator</code>). Depends
  1492. on <code>addon/lint/lint.css</code>. A demo can be
  1493. found <a href="../demo/lint.html">here</a>.</dd>
  1494. <dt id="addon_mark-selection"><a href="../addon/selection/mark-selection.js"><code>selection/mark-selection.js</code></a></dt>
  1495. <dd>Causes the selected text to be marked with the CSS class
  1496. <code>CodeMirror-selectedtext</code> when the <code>styleSelectedText</code> option
  1497. is enabled. Useful to change the colour of the selection (in addition to the background),
  1498. like in <a href="../demo/markselection.html">this demo</a>.</dd>
  1499. <dt id="addon_active-line"><a href="../addon/selection/active-line.js"><code>selection/active-line.js</code></a></dt>
  1500. <dd>Defines a <code>styleActiveLine</code> option that, when enabled,
  1501. gives the wrapper of the active line the class <code>CodeMirror-activeline</code>,
  1502. and adds a background with the class <code>CodeMirror-activeline-background</code>.
  1503. is enabled. See the <a href="../demo/activeline.html">demo</a>.</dd>
  1504. <dt id="addon_closetag"><a href="../addon/edit/closetag.js"><code>edit/closetag.js</code></a></dt>
  1505. <dd>Provides utility functions for adding automatic tag closing
  1506. to XML modes. See
  1507. the <a href="../demo/closetag.html">demo</a>.</dd>
  1508. <dt id="addon_loadmode"><a href="../addon/mode/loadmode.js"><code>mode/loadmode.js</code></a></dt>
  1509. <dd>Defines a <code>CodeMirror.requireMode(modename,
  1510. callback)</code> function that will try to load a given mode and
  1511. call the callback when it succeeded. You'll have to
  1512. set <code>CodeMirror.modeURL</code> to a string that mode paths
  1513. can be constructed from, for
  1514. example <code>"mode/%N/%N.js"</code>—the <code>%N</code>'s will
  1515. be replaced with the mode name. Also
  1516. defines <code>CodeMirror.autoLoadMode(instance, mode)</code>,
  1517. which will ensure the given mode is loaded and cause the given
  1518. editor instance to refresh its mode when the loading
  1519. succeeded. See the <a href="../demo/loadmode.html">demo</a>.</dd>
  1520. <dt id="addon_continuecomment"><a href="../addon/edit/continuecomment.js"><code>edit/continuecomment.js</code></a></dt>
  1521. <dd>Adds an <code>continueComments</code> option, which can be
  1522. set to true to have the editor prefix new lines inside C-like
  1523. block comments with an asterisk when Enter is pressed. It can
  1524. also be set to a string in order to bind this functionality to a
  1525. specific key..</dd>
  1526. <dt id="addon_placeholder"><a href="../addon/display/placeholder.js"><code>display/placeholder.js</code></a></dt>
  1527. <dd>Adds a <code>placeholder</code> option that can be used to
  1528. make text appear in the editor when it is empty and not focused.
  1529. Also gives the editor a <code>CodeMirror-empty</code> CSS class
  1530. whenever it doesn't contain any text.
  1531. See <a href="../demo/placeholder.html">the demo</a>.</dd>
  1532. </dl>
  1533. <h2 id="modeapi">Writing CodeMirror Modes</h2>
  1534. <p>Modes typically consist of a single JavaScript file. This file
  1535. defines, in the simplest case, a lexer (tokenizer) for your
  1536. language—a function that takes a character stream as input,
  1537. advances it past a token, and returns a style for that token. More
  1538. advanced modes can also handle indentation for the language.</p>
  1539. <p id="defineMode">The mode script should
  1540. call <code>CodeMirror.defineMode</code> to register itself with
  1541. CodeMirror. This function takes two arguments. The first should be
  1542. the name of the mode, for which you should use a lowercase string,
  1543. preferably one that is also the name of the files that define the
  1544. mode (i.e. <code>"xml"</code> is defined in <code>xml.js</code>). The
  1545. second argument should be a function that, given a CodeMirror
  1546. configuration object (the thing passed to
  1547. the <code>CodeMirror</code> function) and an optional mode
  1548. configuration object (as in
  1549. the <a href="#option_mode"><code>mode</code></a> option), returns
  1550. a mode object.</p>
  1551. <p>Typically, you should use this second argument
  1552. to <code>defineMode</code> as your module scope function (modes
  1553. should not leak anything into the global scope!), i.e. write your
  1554. whole mode inside this function.</p>
  1555. <p>The main responsibility of a mode script is <em>parsing</em>
  1556. the content of the editor. Depending on the language and the
  1557. amount of functionality desired, this can be done in really easy
  1558. or extremely complicated ways. Some parsers can be stateless,
  1559. meaning that they look at one element (<em>token</em>) of the code
  1560. at a time, with no memory of what came before. Most, however, will
  1561. need to remember something. This is done by using a <em>state
  1562. object</em>, which is an object that is always passed when
  1563. reading a token, and which can be mutated by the tokenizer.</p>
  1564. <p id="startState">Modes that use a state must define
  1565. a <code>startState</code> method on their mode object. This is a
  1566. function of no arguments that produces a state object to be used
  1567. at the start of a document.</p>
  1568. <p id="token">The most important part of a mode object is
  1569. its <code>token(stream, state)</code> method. All modes must
  1570. define this method. It should read one token from the stream it is
  1571. given as an argument, optionally update its state, and return a
  1572. style string, or <code>null</code> for tokens that do not have to
  1573. be styled. For your styles, you are encouraged to use the
  1574. 'standard' names defined in the themes (without
  1575. the <code>cm-</code> prefix). If that fails, it is also possible
  1576. to come up with your own and write your own CSS theme file.<p>
  1577. <p id="StringStream">The stream object that's passed
  1578. to <code>token</code> encapsulates a line of code (tokens may
  1579. never span lines) and our current position in that line. It has
  1580. the following API:</p>
  1581. <dl>
  1582. <dt><code><strong></strong><strong>eol</strong>() → boolean</code></dt>
  1583. <dd>Returns true only if the stream is at the end of the
  1584. line.</dd>
  1585. <dt><code><strong></strong><strong>sol</strong>() → boolean</code></dt>
  1586. <dd>Returns true only if the stream is at the start of the
  1587. line.</dd>
  1588. <dt><code><strong>peek</strong>() → string</code></dt>
  1589. <dd>Returns the next character in the stream without advancing
  1590. it. Will return an <code>null</code> at the end of the
  1591. line.</dd>
  1592. <dt><code><strong>next</strong>() → string</code></dt>
  1593. <dd>Returns the next character in the stream and advances it.
  1594. Also returns <code>null</code> when no more characters are
  1595. available.</dd>
  1596. <dt><code><strong>eat</strong>(match: string|regexp|function(char: string) → boolean) → string</code></dt>
  1597. <dd><code>match</code> can be a character, a regular expression,
  1598. or a function that takes a character and returns a boolean. If
  1599. the next character in the stream 'matches' the given argument,
  1600. it is consumed and returned. Otherwise, <code>undefined</code>
  1601. is returned.</dd>
  1602. <dt><code><strong>eatWhile</strong>(match: string|regexp|function(char: string) → boolean) → boolean</code></dt>
  1603. <dd>Repeatedly calls <code>eat</code> with the given argument,
  1604. until it fails. Returns true if any characters were eaten.</dd>
  1605. <dt><code><strong>eatSpace</strong>() → boolean</code></dt>
  1606. <dd>Shortcut for <code>eatWhile</code> when matching
  1607. white-space.</dd>
  1608. <dt><code><strong>skipToEnd</strong>()</code></dt>
  1609. <dd>Moves the position to the end of the line.</dd>
  1610. <dt><code><strong>skipTo</strong>(ch: string) → boolean</code></dt>
  1611. <dd>Skips to the next occurrence of the given character, if
  1612. found on the current line (doesn't advance the stream if the
  1613. character does not occur on the line). Returns true if the
  1614. character was found.</dd>
  1615. <dt><code><strong>match</strong>(pattern: string, ?consume: boolean, ?caseFold: boolean) → boolean</code></dt>
  1616. <dt><code><strong>match</strong>(pattern: regexp, ?consume: boolean) → array&lt;string&gt;</code></dt>
  1617. <dd>Act like a
  1618. multi-character <code>eat</code>—if <code>consume</code> is true
  1619. or not given—or a look-ahead that doesn't update the stream
  1620. position—if it is false. <code>pattern</code> can be either a
  1621. string or a regular expression starting with <code>^</code>.
  1622. When it is a string, <code>caseFold</code> can be set to true to
  1623. make the match case-insensitive. When successfully matching a
  1624. regular expression, the returned value will be the array
  1625. returned by <code>match</code>, in case you need to extract
  1626. matched groups.</dd>
  1627. <dt><code><strong>backUp</strong>(n: integer)</code></dt>
  1628. <dd>Backs up the stream <code>n</code> characters. Backing it up
  1629. further than the start of the current token will cause things to
  1630. break, so be careful.</dd>
  1631. <dt><code><strong>column</strong>() → integer</code></dt>
  1632. <dd>Returns the column (taking into account tabs) at which the
  1633. current token starts.</dd>
  1634. <dt><code><strong>indentation</strong>() → integer</code></dt>
  1635. <dd>Tells you how far the current line has been indented, in
  1636. spaces. Corrects for tab characters.</dd>
  1637. <dt><code><strong>current</strong>() → string</code></dt>
  1638. <dd>Get the string between the start of the current token and
  1639. the current stream position.</dd>
  1640. </dl>
  1641. <p id="blankLine">By default, blank lines are simply skipped when
  1642. tokenizing a document. For languages that have significant blank
  1643. lines, you can define a <code>blankLine(state)</code> method on
  1644. your mode that will get called whenever a blank line is passed
  1645. over, so that it can update the parser state.</p>
  1646. <p id="copyState">Because state object are mutated, and CodeMirror
  1647. needs to keep valid versions of a state around so that it can
  1648. restart a parse at any line, copies must be made of state objects.
  1649. The default algorithm used is that a new state object is created,
  1650. which gets all the properties of the old object. Any properties
  1651. which hold arrays get a copy of these arrays (since arrays tend to
  1652. be used as mutable stacks). When this is not correct, for example
  1653. because a mode mutates non-array properties of its state object, a
  1654. mode object should define a <code>copyState</code> method,
  1655. which is given a state and should return a safe copy of that
  1656. state.</p>
  1657. <p id="indent">If you want your mode to provide smart indentation
  1658. (through the <a href="#indentLine"><code>indentLine</code></a>
  1659. method and the <code>indentAuto</code>
  1660. and <code>newlineAndIndent</code> commands, to which keys can be
  1661. <a href="#option_extraKeys">bound</a>), you must define
  1662. an <code>indent(state, textAfter)</code> method on your mode
  1663. object.</p>
  1664. <p>The indentation method should inspect the given state object,
  1665. and optionally the <code>textAfter</code> string, which contains
  1666. the text on the line that is being indented, and return an
  1667. integer, the amount of spaces to indent. It should usually take
  1668. the <a href="#option_indentUnit"><code>indentUnit</code></a>
  1669. option into account. An indentation method may
  1670. return <code>CodeMirror.Pass</code> to indicate that it
  1671. could not come up with a precise indentation.</p>
  1672. <p id="mode_comment">To work well with
  1673. the <a href="#addon_comment">commenting addon</a>, a mode may
  1674. define <code>lineComment</code> (string that starts a line
  1675. comment), <code>blockCommentStart</code>, <code>blockCommentEnd</code>
  1676. (strings that start and end block comments),
  1677. and <code>blockCommentLead</code> (a string to put at the start of
  1678. continued lines in a block comment). All of these are
  1679. optional.</p>
  1680. <p id="electricChars">Finally, a mode may define
  1681. an <code>electricChars</code> property, which should hold a string
  1682. containing all the characters that should trigger the behaviour
  1683. described for
  1684. the <a href="#option_electricChars"><code>electricChars</code></a>
  1685. option.</p>
  1686. <p>So, to summarize, a mode <em>must</em> provide
  1687. a <code>token</code> method, and it <em>may</em>
  1688. provide <code>startState</code>, <code>copyState</code>,
  1689. and <code>indent</code> methods. For an example of a trivial mode,
  1690. see the <a href="../mode/diff/diff.js">diff mode</a>, for a more
  1691. involved example, see the <a href="../mode/clike/clike.js">C-like
  1692. mode</a>.</p>
  1693. <p>Sometimes, it is useful for modes to <em>nest</em>—to have one
  1694. mode delegate work to another mode. An example of this kind of
  1695. mode is the <a href="../mode/htmlmixed/htmlmixed.js">mixed-mode HTML
  1696. mode</a>. To implement such nesting, it is usually necessary to
  1697. create mode objects and copy states yourself. To create a mode
  1698. object, there are <code>CodeMirror.getMode(options,
  1699. parserConfig)</code>, where the first argument is a configuration
  1700. object as passed to the mode constructor function, and the second
  1701. argument is a mode specification as in
  1702. the <a href="#option_mode"><code>mode</code></a> option. To copy a
  1703. state object, call <code>CodeMirror.copyState(mode, state)</code>,
  1704. where <code>mode</code> is the mode that created the given
  1705. state.</p>
  1706. <p id="innerMode">In a nested mode, it is recommended to add an
  1707. extra methods, <code>innerMode</code> which, given a state object,
  1708. returns a <code>{state, mode}</code> object with the inner mode
  1709. and its state for the current position. These are used by utility
  1710. scripts such as the <a href="#addon_closetag">tag closer</a> to
  1711. get context information. Use the <code>CodeMirror.innerMode</code>
  1712. helper function to, starting from a mode and a state, recursively
  1713. walk down to the innermost mode and state.</p>
  1714. <p>To make indentation work properly in a nested parser, it is
  1715. advisable to give the <code>startState</code> method of modes that
  1716. are intended to be nested an optional argument that provides the
  1717. base indentation for the block of code. The JavaScript and CSS
  1718. parser do this, for example, to allow JavaScript and CSS code
  1719. inside the mixed-mode HTML mode to be properly indented.</p>
  1720. <p>It is possible, and encouraged, to associate your mode, or a
  1721. certain configuration of your mode, with
  1722. a <a href="http://en.wikipedia.org/wiki/MIME">MIME</a> type. For
  1723. example, the JavaScript mode associates itself
  1724. with <code>text/javascript</code>, and its JSON variant
  1725. with <code>application/json</code>. To do this,
  1726. call <code>CodeMirror.defineMIME(mime, modeSpec)</code>,
  1727. where <code>modeSpec</code> can be a string or object specifying a
  1728. mode, as in the <a href="#option_mode"><code>mode</code></a>
  1729. option.</p>
  1730. <p id="extendMode">Sometimes, it is useful to add or override mode
  1731. object properties from external code.
  1732. The <code>CodeMirror.extendMode</code> can be used to add
  1733. properties to mode objects produced for a specific mode. Its first
  1734. argument is the name of the mode, its second an object that
  1735. specifies the properties that should be added. This is mostly
  1736. useful to add utilities that can later be looked
  1737. up through <a href="#getMode"><code>getMode</code></a>.</p>
  1738. </div><div class="rightsmall blk">
  1739. <h2>Contents</h2>
  1740. <ul>
  1741. <li><a href="#overview">Overview</a></li>
  1742. <li><a href="#usage">Basic Usage</a></li>
  1743. <li><a href="#config">Configuration</a></li>
  1744. <li><a href="#events">Events</a></li>
  1745. <li><a href="#keymaps">Keymaps</a></li>
  1746. <li><a href="#styling">Customized Styling</a></li>
  1747. <li><a href="#api">Programming API</a>
  1748. <ul>
  1749. <li><a href="#constructor">Constructor</a></li>
  1750. <li><a href="#api_content">Content manipulation</a></li>
  1751. <li><a href="#api_selection">Selection</a></li>
  1752. <li><a href="#api_configuration">Configuration</a></li>
  1753. <li><a href="#api_doc">Document management</a></li>
  1754. <li><a href="#api_history">History</a></li>
  1755. <li><a href="#api_marker">Text-marking</a></li>
  1756. <li><a href="#api_decoration">Widget, gutter, and decoration</a></li>
  1757. <li><a href="#api_sizing">Sizing, scrolling, and positioning</a></li>
  1758. <li><a href="#api_mode">Mode, state, and tokens</a></li>
  1759. <li><a href="#api_misc">Miscellaneous methods</a></li>
  1760. <li><a href="#api_static">Static properties</a></li>
  1761. </ul>
  1762. </li>
  1763. <li><a href="#addons">Add-ons</a></li>
  1764. <li><a href="#modeapi">Writing CodeMirror Modes</a></li>
  1765. </ul>
  1766. </div></div>
  1767. <div style="height: 2em">&nbsp;</div>
  1768. <script>setTimeout(function(){CodeMirror.colorize();}, 20);</script>
  1769. </body>
  1770. </html>