manual.html 165 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951
  1. <!doctype html>
  2. <title>CodeMirror: User Manual</title>
  3. <meta charset="utf-8"/>
  4. <link rel=stylesheet href="docs.css">
  5. <script src="activebookmark.js"></script>
  6. <script src="../lib/codemirror.js"></script>
  7. <link rel="stylesheet" href="../lib/codemirror.css">
  8. <script src="../addon/runmode/runmode.js"></script>
  9. <script src="../addon/runmode/colorize.js"></script>
  10. <script src="../mode/javascript/javascript.js"></script>
  11. <script src="../mode/xml/xml.js"></script>
  12. <script src="../mode/css/css.js"></script>
  13. <script src="../mode/htmlmixed/htmlmixed.js"></script>
  14. <style>
  15. dt { text-indent: -2em; padding-left: 2em; margin-top: 1em; }
  16. dd { margin-left: 1.5em; margin-bottom: 1em; }
  17. dt {margin-top: 1em;}
  18. dd dl, dd dt, dd dd, dd ul { margin-top: 0; margin-bottom: 0; }
  19. dt + dt { margin-top: 0; }
  20. dt.command { position: relative; }
  21. span.keybinding { position: absolute; right: 0; font-size: 80%; color: #555; text-indent: 0; }
  22. </style>
  23. <div id=nav>
  24. <a href="http://codemirror.net"><img id=logo src="logo.png"></a>
  25. <ul>
  26. <li><a href="../index.html">Home</a></li>
  27. <li><a href="#overview" class=active data-default="true">Manual</a></li>
  28. <li><a href="https://github.com/marijnh/codemirror">Code</a></li>
  29. </ul>
  30. <ul>
  31. <li><a href="#usage">Basic Usage</a></li>
  32. <li><a href="#config">Configuration</a></li>
  33. <li><a href="#events">Events</a></li>
  34. <li><a href="#keymaps">Key maps</a></li>
  35. <li><a href="#commands">Commands</a></li>
  36. <li><a href="#styling">Customized Styling</a></li>
  37. <li><a href="#api">Programming API</a>
  38. <ul>
  39. <li><a href="#api_constructor">Constructor</a></li>
  40. <li><a href="#api_content">Content manipulation</a></li>
  41. <li><a href="#api_selection">Selection</a></li>
  42. <li><a href="#api_configuration">Configuration</a></li>
  43. <li><a href="#api_doc">Document management</a></li>
  44. <li><a href="#api_history">History</a></li>
  45. <li><a href="#api_marker">Text-marking</a></li>
  46. <li><a href="#api_decoration">Widget, gutter, and decoration</a></li>
  47. <li><a href="#api_sizing">Sizing, scrolling, and positioning</a></li>
  48. <li><a href="#api_mode">Mode, state, and tokens</a></li>
  49. <li><a href="#api_misc">Miscellaneous methods</a></li>
  50. <li><a href="#api_static">Static properties</a></li>
  51. </ul>
  52. </li>
  53. <li><a href="#addons">Addons</a></li>
  54. <li><a href="#modeapi">Writing CodeMirror Modes</a></li>
  55. </ul>
  56. </div>
  57. <article>
  58. <section class=first id=overview>
  59. <h2 style="position: relative">
  60. User manual and reference guide
  61. <span style="color: #888; font-size: 1rem; position: absolute; right: 0; bottom: 0">version 4.6.0</span>
  62. </h2>
  63. <p>CodeMirror is a code-editor component that can be embedded in
  64. Web pages. The core library provides <em>only</em> the editor
  65. component, no accompanying buttons, auto-completion, or other IDE
  66. functionality. It does provide a rich API on top of which such
  67. functionality can be straightforwardly implemented. See
  68. the <a href="#addons">addons</a> included in the distribution,
  69. and the <a href="https://github.com/marijnh/CodeMirror/wiki/CodeMirror-addons">list
  70. of externally hosted addons</a>, for reusable
  71. implementations of extra features.</p>
  72. <p>CodeMirror works with language-specific modes. Modes are
  73. JavaScript programs that help color (and optionally indent) text
  74. written in a given language. The distribution comes with a number
  75. of modes (see the <a href="../mode/"><code>mode/</code></a>
  76. directory), and it isn't hard to <a href="#modeapi">write new
  77. ones</a> for other languages.</p>
  78. </section>
  79. <section id=usage>
  80. <h2>Basic Usage</h2>
  81. <p>The easiest way to use CodeMirror is to simply load the script
  82. and style sheet found under <code>lib/</code> in the distribution,
  83. plus a mode script from one of the <code>mode/</code> directories.
  84. (See <a href="compress.html">the compression helper</a> for an
  85. easy way to combine scripts.) For example:</p>
  86. <pre data-lang="text/html">&lt;script src="lib/codemirror.js">&lt;/script>
  87. &lt;link rel="stylesheet" href="../lib/codemirror.css">
  88. &lt;script src="mode/javascript/javascript.js">&lt;/script></pre>
  89. <p>(Alternatively, use a module loader. <a href="#modloader">More
  90. about that later.</a>)</p>
  91. <p>Having done this, an editor instance can be created like
  92. this:</p>
  93. <pre data-lang="javascript">var myCodeMirror = CodeMirror(document.body);</pre>
  94. <p>The editor will be appended to the document body, will start
  95. empty, and will use the mode that we loaded. To have more control
  96. over the new editor, a configuration object can be passed
  97. to <a href="#CodeMirror"><code>CodeMirror</code></a> as a second
  98. argument:</p>
  99. <pre data-lang="javascript">var myCodeMirror = CodeMirror(document.body, {
  100. value: "function myScript(){return 100;}\n",
  101. mode: "javascript"
  102. });</pre>
  103. <p>This will initialize the editor with a piece of code already in
  104. it, and explicitly tell it to use the JavaScript mode (which is
  105. useful when multiple modes are loaded).
  106. See <a href="#config">below</a> for a full discussion of the
  107. configuration options that CodeMirror accepts.</p>
  108. <p>In cases where you don't want to append the editor to an
  109. element, and need more control over the way it is inserted, the
  110. first argument to the <code>CodeMirror</code> function can also
  111. be a function that, when given a DOM element, inserts it into the
  112. document somewhere. This could be used to, for example, replace a
  113. textarea with a real editor:</p>
  114. <pre data-lang="javascript">var myCodeMirror = CodeMirror(function(elt) {
  115. myTextArea.parentNode.replaceChild(elt, myTextArea);
  116. }, {value: myTextArea.value});</pre>
  117. <p>However, for this use case, which is a common way to use
  118. CodeMirror, the library provides a much more powerful
  119. shortcut:</p>
  120. <pre data-lang="javascript">var myCodeMirror = CodeMirror.fromTextArea(myTextArea);</pre>
  121. <p>This will, among other things, ensure that the textarea's value
  122. is updated with the editor's contents when the form (if it is part
  123. of a form) is submitted. See the <a href="#fromTextArea">API
  124. reference</a> for a full description of this method.</p>
  125. <h3 id=modloader>Module loaders</h3>
  126. <p>The files in the CodeMirror distribution contain shims for
  127. loading them (and their dependencies) in AMD or CommonJS
  128. environments. If the variables <code>exports</code>
  129. and <code>module</code> exist and have type object, CommonJS-style
  130. require will be used. If not, but there is a
  131. function <code>define</code> with an <code>amd</code> property
  132. present, AMD-style (RequireJS) will be used.</p>
  133. <p>It is possible to
  134. use <a href="http://browserify.org/">Browserify</a> or similar
  135. tools to statically build modules using CodeMirror. Alternatively,
  136. use <a href="http://requirejs.org/">RequireJS</a> to dynamically
  137. load dependencies at runtime. Both of these approaches have the
  138. advantage that they don't use the global namespace and can, thus,
  139. do things like load multiple versions of CodeMirror alongside each
  140. other.</p>
  141. <p>Here's a simple example of using RequireJS to load CodeMirror:</p>
  142. <pre data-lang="javascript">require([
  143. "cm/lib/codemirror", "cm/mode/htmlmixed/htmlmixed"
  144. ], function(CodeMirror) {
  145. CodeMirror.fromTextArea(document.getElementById("code"), {
  146. lineNumbers: true,
  147. mode: "htmlmixed"
  148. });
  149. });</pre>
  150. <p>It will automatically load the modes that the mixed HTML mode
  151. depends on (XML, JavaScript, and CSS).</p>
  152. </section>
  153. <section id=config>
  154. <h2>Configuration</h2>
  155. <p>Both the <a href="#CodeMirror"><code>CodeMirror</code></a>
  156. function and its <code>fromTextArea</code> method take as second
  157. (optional) argument an object containing configuration options.
  158. Any option not supplied like this will be taken
  159. from <a href="#defaults"><code>CodeMirror.defaults</code></a>, an
  160. object containing the default options. You can update this object
  161. to change the defaults on your page.</p>
  162. <p>Options are not checked in any way, so setting bogus option
  163. values is bound to lead to odd errors.</p>
  164. <p>These are the supported options:</p>
  165. <dl>
  166. <dt id="option_value"><code><strong>value</strong>: string|CodeMirror.Doc</code></dt>
  167. <dd>The starting value of the editor. Can be a string, or
  168. a <a href="#api_doc">document object</a>.</dd>
  169. <dt id="option_mode"><code><strong>mode</strong>: string|object</code></dt>
  170. <dd>The mode to use. When not given, this will default to the
  171. first mode that was loaded. It may be a string, which either
  172. simply names the mode or is
  173. a <a href="http://en.wikipedia.org/wiki/MIME">MIME</a> type
  174. associated with the mode. Alternatively, it may be an object
  175. containing configuration options for the mode, with
  176. a <code>name</code> property that names the mode (for
  177. example <code>{name: "javascript", json: true}</code>). The demo
  178. pages for each mode contain information about what configuration
  179. parameters the mode supports. You can ask CodeMirror which modes
  180. and MIME types have been defined by inspecting
  181. the <code>CodeMirror.modes</code>
  182. and <code>CodeMirror.mimeModes</code> objects. The first maps
  183. mode names to their constructors, and the second maps MIME types
  184. to mode specs.</dd>
  185. <dt id="option_theme"><code><strong>theme</strong>: string</code></dt>
  186. <dd>The theme to style the editor with. You must make sure the
  187. CSS file defining the corresponding <code>.cm-s-[name]</code>
  188. styles is loaded (see
  189. the <a href="../theme/"><code>theme</code></a> directory in the
  190. distribution). The default is <code>"default"</code>, for which
  191. colors are included in <code>codemirror.css</code>. It is
  192. possible to use multiple theming classes at once—for
  193. example <code>"foo bar"</code> will assign both
  194. the <code>cm-s-foo</code> and the <code>cm-s-bar</code> classes
  195. to the editor.</dd>
  196. <dt id="option_indentUnit"><code><strong>indentUnit</strong>: integer</code></dt>
  197. <dd>How many spaces a block (whatever that means in the edited
  198. language) should be indented. The default is 2.</dd>
  199. <dt id="option_smartIndent"><code><strong>smartIndent</strong>: boolean</code></dt>
  200. <dd>Whether to use the context-sensitive indentation that the
  201. mode provides (or just indent the same as the line before).
  202. Defaults to true.</dd>
  203. <dt id="option_tabSize"><code><strong>tabSize</strong>: integer</code></dt>
  204. <dd>The width of a tab character. Defaults to 4.</dd>
  205. <dt id="option_indentWithTabs"><code><strong>indentWithTabs</strong>: boolean</code></dt>
  206. <dd>Whether, when indenting, the first N*<code>tabSize</code>
  207. spaces should be replaced by N tabs. Default is false.</dd>
  208. <dt id="option_electricChars"><code><strong>electricChars</strong>: boolean</code></dt>
  209. <dd>Configures whether the editor should re-indent the current
  210. line when a character is typed that might change its proper
  211. indentation (only works if the mode supports indentation).
  212. Default is true.</dd>
  213. <dt id="option_specialChars"><code><strong>specialChars</strong>: RegExp</code></dt>
  214. <dd>A regular expression used to determine which characters
  215. should be replaced by a
  216. special <a href="#option_specialCharPlaceholder">placeholder</a>.
  217. Mostly useful for non-printing special characters. The default
  218. is <code>/[\u0000-\u0019\u00ad\u200b\u2028\u2029\ufeff]/</code>.</dd>
  219. <dt id="option_specialCharPlaceholder"><code><strong>specialCharPlaceholder</strong>: function(char) → Element</code></dt>
  220. <dd>A function that, given a special character identified by
  221. the <a href="#option_specialChars"><code>specialChars</code></a>
  222. option, produces a DOM node that is used to represent the
  223. character. By default, a red dot (<span style="color: red">•</span>)
  224. is shown, with a title tooltip to indicate the character code.</dd>
  225. <dt id="option_rtlMoveVisually"><code><strong>rtlMoveVisually</strong>: boolean</code></dt>
  226. <dd>Determines whether horizontal cursor movement through
  227. right-to-left (Arabic, Hebrew) text is visual (pressing the left
  228. arrow moves the cursor left) or logical (pressing the left arrow
  229. moves to the next lower index in the string, which is visually
  230. right in right-to-left text). The default is <code>false</code>
  231. on Windows, and <code>true</code> on other platforms.</dd>
  232. <dt id="option_keyMap"><code><strong>keyMap</strong>: string</code></dt>
  233. <dd>Configures the key map to use. The default
  234. is <code>"default"</code>, which is the only key map defined
  235. in <code>codemirror.js</code> itself. Extra key maps are found in
  236. the <a href="../keymap/"><code>key map</code></a> directory. See
  237. the <a href="#keymaps">section on key maps</a> for more
  238. information.</dd>
  239. <dt id="option_extraKeys"><code><strong>extraKeys</strong>: object</code></dt>
  240. <dd>Can be used to specify extra key bindings for the editor,
  241. alongside the ones defined
  242. by <a href="#option_keyMap"><code>keyMap</code></a>. Should be
  243. either null, or a valid <a href="#keymaps">key map</a> value.</dd>
  244. <dt id="option_lineWrapping"><code><strong>lineWrapping</strong>: boolean</code></dt>
  245. <dd>Whether CodeMirror should scroll or wrap for long lines.
  246. Defaults to <code>false</code> (scroll).</dd>
  247. <dt id="option_lineNumbers"><code><strong>lineNumbers</strong>: boolean</code></dt>
  248. <dd>Whether to show line numbers to the left of the editor.</dd>
  249. <dt id="option_firstLineNumber"><code><strong>firstLineNumber</strong>: integer</code></dt>
  250. <dd>At which number to start counting lines. Default is 1.</dd>
  251. <dt id="option_lineNumberFormatter"><code><strong>lineNumberFormatter</strong>: function(line: integer) → string</code></dt>
  252. <dd>A function used to format line numbers. The function is
  253. passed the line number, and should return a string that will be
  254. shown in the gutter.</dd>
  255. <dt id="option_gutters"><code><strong>gutters</strong>: array&lt;string&gt;</code></dt>
  256. <dd>Can be used to add extra gutters (beyond or instead of the
  257. line number gutter). Should be an array of CSS class names, each
  258. of which defines a <code>width</code> (and optionally a
  259. background), and which will be used to draw the background of
  260. the gutters. <em>May</em> include
  261. the <code>CodeMirror-linenumbers</code> class, in order to
  262. explicitly set the position of the line number gutter (it will
  263. default to be to the right of all other gutters). These class
  264. names are the keys passed
  265. to <a href="#setGutterMarker"><code>setGutterMarker</code></a>.</dd>
  266. <dt id="option_fixedGutter"><code><strong>fixedGutter</strong>: boolean</code></dt>
  267. <dd>Determines whether the gutter scrolls along with the content
  268. horizontally (false) or whether it stays fixed during horizontal
  269. scrolling (true, the default).</dd>
  270. <dt id="option_coverGutterNextToScrollbar"><code><strong>coverGutterNextToScrollbar</strong>: boolean</code></dt>
  271. <dd>When <a href="#option_fixedGutter"><code>fixedGutter</code></a>
  272. is on, and there is a horizontal scrollbar, by default the
  273. gutter will be visible to the left of this scrollbar. If this
  274. option is set to true, it will be covered by an element with
  275. class <code>CodeMirror-gutter-filler</code>.</dd>
  276. <dt id="option_readOnly"><code><strong>readOnly</strong>: boolean|string</code></dt>
  277. <dd>This disables editing of the editor content by the user. If
  278. the special value <code>"nocursor"</code> is given (instead of
  279. simply <code>true</code>), focusing of the editor is also
  280. disallowed.</dd>
  281. <dt id="option_showCursorWhenSelecting"><code><strong>showCursorWhenSelecting</strong>: boolean</code></dt>
  282. <dd>Whether the cursor should be drawn when a selection is
  283. active. Defaults to false.</dd>
  284. <dt id="option_undoDepth"><code><strong>undoDepth</strong>: integer</code></dt>
  285. <dd>The maximum number of undo levels that the editor stores.
  286. Note that this includes selection change events. Defaults to
  287. 200.</dd>
  288. <dt id="option_historyEventDelay"><code><strong>historyEventDelay</strong>: integer</code></dt>
  289. <dd>The period of inactivity (in milliseconds) that will cause a
  290. new history event to be started when typing or deleting.
  291. Defaults to 1250.</dd>
  292. <dt id="option_tabindex"><code><strong>tabindex</strong>: integer</code></dt>
  293. <dd>The <a href="http://www.w3.org/TR/html401/interact/forms.html#adef-tabindex">tab
  294. index</a> to assign to the editor. If not given, no tab index
  295. will be assigned.</dd>
  296. <dt id="option_autofocus"><code><strong>autofocus</strong>: boolean</code></dt>
  297. <dd>Can be used to make CodeMirror focus itself on
  298. initialization. Defaults to off.
  299. When <a href="#fromTextArea"><code>fromTextArea</code></a> is
  300. used, and no explicit value is given for this option, it will be
  301. set to true when either the source textarea is focused, or it
  302. has an <code>autofocus</code> attribute and no other element is
  303. focused.</dd>
  304. </dl>
  305. <p>Below this a few more specialized, low-level options are
  306. listed. These are only useful in very specific situations, you
  307. might want to skip them the first time you read this manual.</p>
  308. <dl>
  309. <dt id="option_dragDrop"><code><strong>dragDrop</strong>: boolean</code></dt>
  310. <dd>Controls whether drag-and-drop is enabled. On by default.</dd>
  311. <dt id="option_cursorBlinkRate"><code><strong>cursorBlinkRate</strong>: number</code></dt>
  312. <dd>Half-period in milliseconds used for cursor blinking. The default blink
  313. rate is 530ms. By setting this to zero, blinking can be disabled. A
  314. negative value hides the cursor entirely.</dd>
  315. <dt id="option_cursorScrollMargin"><code><strong>cursorScrollMargin</strong>: number</code></dt>
  316. <dd>How much extra space to always keep above and below the
  317. cursor when approaching the top or bottom of the visible view in
  318. a scrollable document. Default is 0.</dd>
  319. <dt id="option_cursorHeight"><code><strong>cursorHeight</strong>: number</code></dt>
  320. <dd>Determines the height of the cursor. Default is 1, meaning
  321. it spans the whole height of the line. For some fonts (and by
  322. some tastes) a smaller height (for example <code>0.85</code>),
  323. which causes the cursor to not reach all the way to the bottom
  324. of the line, looks better</dd>
  325. <dt id="option_resetSelectionOnContextMenu"><code><strong>resetSelectionOnContextMenu</strong>: boolean</code></dt>
  326. <dd>Controls whether, when the context menu is opened with a
  327. click outside of the current selection, the cursor is moved to
  328. the point of the click. Defaults to <code>true</code>.</dd>
  329. <dt id="option_workTime"><code id="option_wordkDelay"><strong>workTime</strong>, <strong>workDelay</strong>: number</code></dt>
  330. <dd>Highlighting is done by a pseudo background-thread that will
  331. work for <code>workTime</code> milliseconds, and then use
  332. timeout to sleep for <code>workDelay</code> milliseconds. The
  333. defaults are 200 and 300, you can change these options to make
  334. the highlighting more or less aggressive.</dd>
  335. <dt id="option_pollInterval"><code><strong>pollInterval</strong>: number</code></dt>
  336. <dd>Indicates how quickly CodeMirror should poll its input
  337. textarea for changes (when focused). Most input is captured by
  338. events, but some things, like IME input on some browsers, don't
  339. generate events that allow CodeMirror to properly detect it.
  340. Thus, it polls. Default is 100 milliseconds.</dd>
  341. <dt id="option_flattenSpans"><code><strong>flattenSpans</strong>: boolean</code></dt>
  342. <dd>By default, CodeMirror will combine adjacent tokens into a
  343. single span if they have the same class. This will result in a
  344. simpler DOM tree, and thus perform better. With some kinds of
  345. styling (such as rounded corners), this will change the way the
  346. document looks. You can set this option to false to disable this
  347. behavior.</dd>
  348. <dt id="option_addModeClass"><code><strong>addModeClass</strong>: boolean</code></dt>
  349. <dd>When enabled (off by default), an extra CSS class will be
  350. added to each token, indicating the
  351. (<a href="#innerMode">inner</a>) mode that produced it, prefixed
  352. with <code>"cm-m-"</code>. For example, tokens from the XML mode
  353. will get the <code>cm-m-xml</code> class.</dd>
  354. <dt id="option_maxHighlightLength"><code><strong>maxHighlightLength</strong>: number</code></dt>
  355. <dd>When highlighting long lines, in order to stay responsive,
  356. the editor will give up and simply style the rest of the line as
  357. plain text when it reaches a certain position. The default is
  358. 10 000. You can set this to <code>Infinity</code> to turn off
  359. this behavior.</dd>
  360. <dt id="option_crudeMeasuringFrom"><code><strong>crudeMeasuringFrom</strong>: number</code></dt>
  361. <dd>When measuring the character positions in long lines, any
  362. line longer than this number (default is 10 000),
  363. when <a href="#option_lineWrapping">line wrapping</a>
  364. is <strong>off</strong>, will simply be assumed to consist of
  365. same-sized characters. This means that, on the one hand,
  366. measuring will be inaccurate when characters of varying size,
  367. right-to-left text, markers, or other irregular elements are
  368. present. On the other hand, it means that having such a line
  369. won't freeze the user interface because of the expensiveness of
  370. the measurements.</dd>
  371. <dt id="option_viewportMargin"><code><strong>viewportMargin</strong>: integer</code></dt>
  372. <dd>Specifies the amount of lines that are rendered above and
  373. below the part of the document that's currently scrolled into
  374. view. This affects the amount of updates needed when scrolling,
  375. and the amount of work that such an update does. You should
  376. usually leave it at its default, 10. Can be set
  377. to <code>Infinity</code> to make sure the whole document is
  378. always rendered, and thus the browser's text search works on it.
  379. This <em>will</em> have bad effects on performance of big
  380. documents.</dd>
  381. </dl>
  382. </section>
  383. <section id=events>
  384. <h2>Events</h2>
  385. <p>Various CodeMirror-related objects emit events, which allow
  386. client code to react to various situations. Handlers for such
  387. events can be registered with the <a href="#on"><code>on</code></a>
  388. and <a href="#off"><code>off</code></a> methods on the objects
  389. that the event fires on. To fire your own events,
  390. use <code>CodeMirror.signal(target, name, args...)</code>,
  391. where <code>target</code> is a non-DOM-node object.</p>
  392. <p>An editor instance fires the following events.
  393. The <code>instance</code> argument always refers to the editor
  394. itself.</p>
  395. <dl>
  396. <dt id="event_change"><code><strong>"change"</strong> (instance: CodeMirror, changeObj: object)</code></dt>
  397. <dd>Fires every time the content of the editor is changed.
  398. The <code>changeObj</code> is a <code>{from, to, text, removed,
  399. origin}</code> object containing information about the changes
  400. that occurred as second argument. <code>from</code>
  401. and <code>to</code> are the positions (in the pre-change
  402. coordinate system) where the change started and ended (for
  403. example, it might be <code>{ch:0, line:18}</code> if the
  404. position is at the beginning of line #19). <code>text</code> is
  405. an array of strings representing the text that replaced the
  406. changed range (split by line). <code>removed</code> is the text
  407. that used to be between <code>from</code> and <code>to</code>,
  408. which is overwritten by this change.</dd>
  409. <dt id="event_changes"><code><strong>"changes"</strong> (instance: CodeMirror, changes: array&lt;object&lt;)</code></dt>
  410. <dd>Like the <a href="#event_change"><code>"change"</code></a>
  411. event, but batched per <a href="#operation">operation</a>,
  412. passing an array containing all the changes that happened in the
  413. operation.</dd>
  414. <dt id="event_beforeChange"><code><strong>"beforeChange"</strong> (instance: CodeMirror, changeObj: object)</code></dt>
  415. <dd>This event is fired before a change is applied, and its
  416. handler may choose to modify or cancel the change.
  417. The <code>changeObj</code> object
  418. has <code>from</code>, <code>to</code>, and <code>text</code>
  419. properties, as with
  420. the <a href="#event_change"><code>"change"</code></a> event. It
  421. also has a <code>cancel()</code> method, which can be called to
  422. cancel the change, and, <strong>if</strong> the change isn't
  423. coming from an undo or redo event, an <code>update(from, to,
  424. text)</code> method, which may be used to modify the change.
  425. Undo or redo changes can't be modified, because they hold some
  426. metainformation for restoring old marked ranges that is only
  427. valid for that specific change. All three arguments
  428. to <code>update</code> are optional, and can be left off to
  429. leave the existing value for that field
  430. intact. <strong>Note:</strong> you may not do anything from
  431. a <code>"beforeChange"</code> handler that would cause changes
  432. to the document or its visualization. Doing so will, since this
  433. handler is called directly from the bowels of the CodeMirror
  434. implementation, probably cause the editor to become
  435. corrupted.</dd>
  436. <dt id="event_cursorActivity"><code><strong>"cursorActivity"</strong> (instance: CodeMirror)</code></dt>
  437. <dd>Will be fired when the cursor or selection moves, or any
  438. change is made to the editor content.</dd>
  439. <dt id="event_keyHandled"><code><strong>"keyHandled"</strong> (instance: CodeMirror, name: string, event: Event)</code></dt>
  440. <dd>Fired after a key is handled through a
  441. key map. <code>name</code> is the name of the handled key (for
  442. example <code>"Ctrl-X"</code> or <code>"'q'"</code>),
  443. and <code>event</code> is the DOM <code>keydown</code>
  444. or <code>keypress</code> event.</dd>
  445. <dt id="event_inputRead"><code><strong>"inputRead"</strong> (instance: CodeMirror, changeObj: object)</code></dt>
  446. <dd>Fired whenever new input is read from the hidden textarea
  447. (typed or pasted by the user).</dd>
  448. <dt id="event_beforeSelectionChange"><code><strong>"beforeSelectionChange"</strong> (instance: CodeMirror, obj: {ranges, update})</code></dt>
  449. <dd>This event is fired before the selection is moved. Its
  450. handler may inspect the set of selection ranges, present as an
  451. array of <code>{anchor, head}</code> objects in
  452. the <code>ranges</code> property of the <code>obj</code>
  453. argument, and optionally change them by calling
  454. the <code>update</code> method on this object, passing an array
  455. of ranges in the same format. Handlers for this event have the
  456. same restriction
  457. as <a href="#event_beforeChange"><code>"beforeChange"</code></a>
  458. handlers — they should not do anything to directly update the
  459. state of the editor.</dd>
  460. <dt id="event_viewportChange"><code><strong>"viewportChange"</strong> (instance: CodeMirror, from: number, to: number)</code></dt>
  461. <dd>Fires whenever the <a href="#getViewport">view port</a> of
  462. the editor changes (due to scrolling, editing, or any other
  463. factor). The <code>from</code> and <code>to</code> arguments
  464. give the new start and end of the viewport.</dd>
  465. <dt id="event_swapDoc"><code><strong>"swapDoc"</strong> (instance: CodeMirror, oldDoc: Doc)</code></dt>
  466. <dd>This is signalled when the editor's document is replaced
  467. using the <a href="#swapDoc"><code>swapDoc</code></a>
  468. method.</dd>
  469. <dt id="event_gutterClick"><code><strong>"gutterClick"</strong> (instance: CodeMirror, line: integer, gutter: string, clickEvent: Event)</code></dt>
  470. <dd>Fires when the editor gutter (the line-number area) is
  471. clicked. Will pass the editor instance as first argument, the
  472. (zero-based) number of the line that was clicked as second
  473. argument, the CSS class of the gutter that was clicked as third
  474. argument, and the raw <code>mousedown</code> event object as
  475. fourth argument.</dd>
  476. <dt id="event_gutterContextMenu"><code><strong>"gutterContextMenu"</strong> (instance: CodeMirror, line: integer, gutter: string, contextMenu: Event: Event)</code></dt>
  477. <dd>Fires when the editor gutter (the line-number area)
  478. receives a <code>contextmenu</code> event. Will pass the editor
  479. instance as first argument, the (zero-based) number of the line
  480. that was clicked as second argument, the CSS class of the
  481. gutter that was clicked as third argument, and the raw
  482. <code>contextmenu</code> mouse event object as fourth argument.
  483. You can <code>preventDefault</code> the event, to signal that
  484. CodeMirror should do no further handling.</dd>
  485. <dt id="event_focus"><code><strong>"focus"</strong> (instance: CodeMirror)</code></dt>
  486. <dd>Fires whenever the editor is focused.</dd>
  487. <dt id="event_blur"><code><strong>"blur"</strong> (instance: CodeMirror)</code></dt>
  488. <dd>Fires whenever the editor is unfocused.</dd>
  489. <dt id="event_scroll"><code><strong>"scroll"</strong> (instance: CodeMirror)</code></dt>
  490. <dd>Fires when the editor is scrolled.</dd>
  491. <dt id="event_update"><code><strong>"update"</strong> (instance: CodeMirror)</code></dt>
  492. <dd>Will be fired whenever CodeMirror updates its DOM display.</dd>
  493. <dt id="event_renderLine"><code><strong>"renderLine"</strong> (instance: CodeMirror, line: LineHandle, element: Element)</code></dt>
  494. <dd>Fired whenever a line is (re-)rendered to the DOM. Fired
  495. right after the DOM element is built, <em>before</em> it is
  496. added to the document. The handler may mess with the style of
  497. the resulting element, or add event handlers, but
  498. should <em>not</em> try to change the state of the editor.</dd>
  499. <dt id="event_dom"><code><strong>"mousedown"</strong>,
  500. <strong>"dblclick"</strong>, <strong>"contextmenu"</strong>, <strong>"keydown"</strong>, <strong>"keypress"</strong>,
  501. <strong>"keyup"</strong>, <strong>"dragstart"</strong>, <strong>"dragenter"</strong>,
  502. <strong>"dragover"</strong>, <strong>"drop"</strong>
  503. (instance: CodeMirror, event: Event)</code></dt>
  504. <dd>Fired when CodeMirror is handling a DOM event of this type.
  505. You can <code>preventDefault</code> the event, or give it a
  506. truthy <code>codemirrorIgnore</code> property, to signal that
  507. CodeMirror should do no further handling.</dd>
  508. </dl>
  509. <p>Document objects (instances
  510. of <a href="#Doc"><code>CodeMirror.Doc</code></a>) emit the
  511. following events:</p>
  512. <dl>
  513. <dt id="event_doc_change"><code><strong>"change"</strong> (doc: CodeMirror.Doc, changeObj: object)</code></dt>
  514. <dd>Fired whenever a change occurs to the
  515. document. <code>changeObj</code> has a similar type as the
  516. object passed to the
  517. editor's <a href="#event_change"><code>"change"</code></a>
  518. event.</dd>
  519. <dt id="event_doc_beforeChange"><code><strong>"beforeChange"</strong> (doc: CodeMirror.Doc, change: object)</code></dt>
  520. <dd>See the <a href="#event_beforeChange">description of the
  521. same event</a> on editor instances.</dd>
  522. <dt id="event_doc_cursorActivity"><code><strong>"cursorActivity"</strong> (doc: CodeMirror.Doc)</code></dt>
  523. <dd>Fired whenever the cursor or selection in this document
  524. changes.</dd>
  525. <dt id="event_doc_beforeSelectionChange"><code><strong>"beforeSelectionChange"</strong> (doc: CodeMirror.Doc, selection: {head, anchor})</code></dt>
  526. <dd>Equivalent to
  527. the <a href="#event_beforeSelectionChange">event by the same
  528. name</a> as fired on editor instances.</dd>
  529. </dl>
  530. <p>Line handles (as returned by, for
  531. example, <a href="#getLineHandle"><code>getLineHandle</code></a>)
  532. support these events:</p>
  533. <dl>
  534. <dt id="event_delete"><code><strong>"delete"</strong> ()</code></dt>
  535. <dd>Will be fired when the line object is deleted. A line object
  536. is associated with the <em>start</em> of the line. Mostly useful
  537. when you need to find out when your <a href="#setGutterMarker">gutter
  538. markers</a> on a given line are removed.</dd>
  539. <dt id="event_line_change"><code><strong>"change"</strong> (line: LineHandle, changeObj: object)</code></dt>
  540. <dd>Fires when the line's text content is changed in any way
  541. (but the line is not deleted outright). The <code>change</code>
  542. object is similar to the one passed
  543. to <a href="#event_change">change event</a> on the editor
  544. object.</dd>
  545. </dl>
  546. <p>Marked range handles (<code>CodeMirror.TextMarker</code>), as returned
  547. by <a href="#markText"><code>markText</code></a>
  548. and <a href="#setBookmark"><code>setBookmark</code></a>, emit the
  549. following events:</p>
  550. <dl>
  551. <dt id="event_beforeCursorEnter"><code><strong>"beforeCursorEnter"</strong> ()</code></dt>
  552. <dd>Fired when the cursor enters the marked range. From this
  553. event handler, the editor state may be inspected
  554. but <em>not</em> modified, with the exception that the range on
  555. which the event fires may be cleared.</dd>
  556. <dt id="event_clear"><code><strong>"clear"</strong> (from: {line, ch}, to: {line, ch})</code></dt>
  557. <dd>Fired when the range is cleared, either through cursor
  558. movement in combination
  559. with <a href="#mark_clearOnEnter"><code>clearOnEnter</code></a>
  560. or through a call to its <code>clear()</code> method. Will only
  561. be fired once per handle. Note that deleting the range through
  562. text editing does not fire this event, because an undo action
  563. might bring the range back into existence. <code>from</code>
  564. and <code>to</code> give the part of the document that the range
  565. spanned when it was cleared.</dd>
  566. <dt id="event_hide"><code><strong>"hide"</strong> ()</code></dt>
  567. <dd>Fired when the last part of the marker is removed from the
  568. document by editing operations.</dd>
  569. <dt id="event_unhide"><code><strong>"unhide"</strong> ()</code></dt>
  570. <dd>Fired when, after the marker was removed by editing, a undo
  571. operation brought the marker back.</dd>
  572. </dl>
  573. <p>Line widgets (<code>CodeMirror.LineWidget</code>), returned
  574. by <a href="#addLineWidget"><code>addLineWidget</code></a>, fire
  575. these events:</p>
  576. <dl>
  577. <dt id="event_redraw"><code><strong>"redraw"</strong> ()</code></dt>
  578. <dd>Fired whenever the editor re-adds the widget to the DOM.
  579. This will happen once right after the widget is added (if it is
  580. scrolled into view), and then again whenever it is scrolled out
  581. of view and back in again, or when changes to the editor options
  582. or the line the widget is on require the widget to be
  583. redrawn.</dd>
  584. </dl>
  585. </section>
  586. <section id=keymaps>
  587. <h2>Key Maps</h2>
  588. <p>Key maps are ways to associate keys with functionality. A key map
  589. is an object mapping strings that identify the keys to functions
  590. that implement their functionality.</p>
  591. <p>The CodeMirror distributions comes
  592. with <a href="../demo/emacs.html">Emacs</a>, <a href="../demo/vim.html">Vim</a>,
  593. and <a href="../demo/sublime.html">Sublime Text</a>-style keymaps.</p>
  594. <p>Keys are identified either by name or by character.
  595. The <code>CodeMirror.keyNames</code> object defines names for
  596. common keys and associates them with their key codes. Examples of
  597. names defined here are <code>Enter</code>, <code>F5</code>,
  598. and <code>Q</code>. These can be prefixed
  599. with <code>Shift-</code>, <code>Cmd-</code>, <code>Ctrl-</code>,
  600. and <code>Alt-</code> (in that order!) to specify a modifier. So
  601. for example, <code>Shift-Ctrl-Space</code> would be a valid key
  602. identifier.</p>
  603. <p>Common example: map the Tab key to insert spaces instead of a tab
  604. character.</p>
  605. <pre data-lang="javascript">
  606. {
  607. Tab: function(cm) {
  608. var spaces = Array(cm.getOption("indentUnit") + 1).join(" ");
  609. cm.replaceSelection(spaces);
  610. }
  611. }</pre>
  612. <p>Alternatively, a character can be specified directly by
  613. surrounding it in single quotes, for example <code>'$'</code>
  614. or <code>'q'</code>. Due to limitations in the way browsers fire
  615. key events, these may not be prefixed with modifiers.</p>
  616. <p>The <code>CodeMirror.keyMap</code> object associates key maps
  617. with names. User code and key map definitions can assign extra
  618. properties to this object. Anywhere where a key map is expected, a
  619. string can be given, which will be looked up in this object. It
  620. also contains the <code>"default"</code> key map holding the
  621. default bindings.</p>
  622. <p>The values of properties in key maps can be either functions of
  623. a single argument (the CodeMirror instance), strings, or
  624. <code>false</code>. Strings refer
  625. to <a href="#commands">commands</a>, which are described below. If
  626. the property is set to <code>false</code>, CodeMirror leaves
  627. handling of the key up to the browser. A key handler function may
  628. return <code>CodeMirror.Pass</code> to indicate that it has
  629. decided not to handle the key, and other handlers (or the default
  630. behavior) should be given a turn.</p>
  631. <p>Keys mapped to command names that start with the
  632. characters <code>"go"</code> (which should be used for
  633. cursor-movement actions) will be fired even when an
  634. extra <code>Shift</code> modifier is present (i.e. <code>"Up":
  635. "goLineUp"</code> matches both up and shift-up). This is used to
  636. easily implement shift-selection.</p>
  637. <p>Key maps can defer to each other by defining
  638. a <code>fallthrough</code> property. This indicates that when a
  639. key is not found in the map itself, one or more other maps should
  640. be searched. It can hold either a single key map or an array of
  641. key maps.</p>
  642. <p>When a key map contains a <code>nofallthrough</code> property
  643. set to <code>true</code>, keys matched against that map will be
  644. ignored if they don't match any of the bindings in the map (no
  645. further child maps will be tried). When
  646. the <code>disableInput</code> property is set
  647. to <code>true</code>, the default effect of inserting a character
  648. will be suppressed when the key map is active as the top-level
  649. map.</p>
  650. </section>
  651. <section id=commands>
  652. <h2>Commands</h2>
  653. <p>Commands are parameter-less actions that can be performed on an
  654. editor. Their main use is for key bindings. Commands are defined by
  655. adding properties to the <code>CodeMirror.commands</code> object.
  656. A number of common commands are defined by the library itself,
  657. most of them used by the default key bindings. The value of a
  658. command property must be a function of one argument (an editor
  659. instance).</p>
  660. <p>Some of the commands below are referenced in the default
  661. key map, but not defined by the core library. These are intended to
  662. be defined by user code or addons.</p>
  663. <p>Commands can also be run with
  664. the <a href="#execCommand"><code>execCommand</code></a>
  665. method.</p>
  666. <dl>
  667. <dt class=command id=command_selectAll><code><strong>selectAll</strong></code><span class=keybinding>Ctrl-A (PC), Cmd-A (Mac)</span></dt>
  668. <dd>Select the whole content of the editor.</dd>
  669. <dt class=command id=command_singleSelection><code><strong>singleSelection</strong></code><span class=keybinding>Esc</span></dt>
  670. <dd>When multiple selections are present, this deselects all but
  671. the primary selection.</dd>
  672. <dt class=command id=command_killLine><code><strong>killLine</strong></code><span class=keybinding>Ctrl-K (Mac)</span></dt>
  673. <dd>Emacs-style line killing. Deletes the part of the line after
  674. the cursor. If that consists only of whitespace, the newline at
  675. the end of the line is also deleted.</dd>
  676. <dt class=command id=command_deleteLine><code><strong>deleteLine</strong></code><span class=keybinding>Ctrl-D (PC), Cmd-D (Mac)</span></dt>
  677. <dd>Deletes the whole line under the cursor, including newline at the end.</dd>
  678. <dt class=command id=command_delLineLeft><code><strong>delLineLeft</strong></code></dt>
  679. <dd>Delete the part of the line before the cursor.</dd>
  680. <dt class=command id=command_delWrappedLineLeft><code><strong>delWrappedLineLeft</strong></code><span class=keybinding>Cmd-Backspace (Mac)</span></dt>
  681. <dd>Delete the part of the line from the left side of the visual line the cursor is on to the cursor.</dd>
  682. <dt class=command id=command_delWrappedLineRight><code><strong>delWrappedLineRight</strong></code><span class=keybinding>Cmd-Delete (Mac)</span></dt>
  683. <dd>Delete the part of the line from the cursor to the right side of the visual line the cursor is on.</dd>
  684. <dt class=command id=command_undo><code><strong>undo</strong></code><span class=keybinding>Ctrl-Z (PC), Cmd-Z (Mac)</span></dt>
  685. <dd>Undo the last change.</dd>
  686. <dt class=command id=command_redo><code><strong>redo</strong></code><span class=keybinding>Ctrl-Y (PC), Shift-Cmd-Z (Mac), Cmd-Y (Mac)</span></dt>
  687. <dd>Redo the last undone change.</dd>
  688. <dt class=command id=command_undoSelection><code><strong>undoSelection</strong></code><span class=keybinding>Ctrl-U (PC), Cmd-U (Mac)</span></dt>
  689. <dd>Undo the last change to the selection, or if there are no
  690. selection-only changes at the top of the history, undo the last
  691. change.</dd>
  692. <dt class=command id=command_redoSelection><code><strong>redoSelection</strong></code><span class=keybinding>Alt-U (PC), Shift-Cmd-U (Mac)</span></dt>
  693. <dd>Redo the last change to the selection, or the last text change if
  694. no selection changes remain.</dd>
  695. <dt class=command id=command_goDocStart><code><strong>goDocStart</strong></code><span class=keybinding>Ctrl-Up (PC), Cmd-Up (Mac), Cmd-Home (Mac)</span></dt>
  696. <dd>Move the cursor to the start of the document.</dd>
  697. <dt class=command id=command_goDocEnd><code><strong>goDocEnd</strong></code><span class=keybinding>Ctrl-Down (PC), Cmd-End (Mac), Cmd-Down (Mac)</span></dt>
  698. <dd>Move the cursor to the end of the document.</dd>
  699. <dt class=command id=command_goLineStart><code><strong>goLineStart</strong></code><span class=keybinding>Alt-Left (PC), Ctrl-A (Mac)</span></dt>
  700. <dd>Move the cursor to the start of the line.</dd>
  701. <dt class=command id=command_goLineStartSmart><code><strong>goLineStartSmart</strong></code><span class=keybinding>Home</span></dt>
  702. <dd>Move to the start of the text on the line, or if we are
  703. already there, to the actual start of the line (including
  704. whitespace).</dd>
  705. <dt class=command id=command_goLineEnd><code><strong>goLineEnd</strong></code><span class=keybinding>Alt-Right (PC), Ctrl-E (Mac)</span></dt>
  706. <dd>Move the cursor to the end of the line.</dd>
  707. <dt class=command id=command_goLineRight><code><strong>goLineRight</strong></code><span class=keybinding>Cmd-Right (Mac)</span></dt>
  708. <dd>Move the cursor to the right side of the visual line it is on.</dd>
  709. <dt class=command id=command_goLineLeft><code><strong>goLineLeft</strong></code><span class=keybinding>Cmd-Left (Mac)</span></dt>
  710. <dd>Move the cursor to the left side of the visual line it is on. If
  711. this line is wrapped, that may not be the start of the line.</dd>
  712. <dt class=command id=command_goLineLeftSmart><code><strong>goLineLeftSmart</strong></code></dt>
  713. <dd>Move the cursor to the left side of the visual line it is
  714. on. If that takes it to the start of the line, behave
  715. like <a href="#command_goLineStartSmart"><code>goLineStartSmart</code></a>.</dd>
  716. <dt class=command id=command_goLineUp><code><strong>goLineUp</strong></code><span class=keybinding>Up, Ctrl-P (Mac)</span></dt>
  717. <dd>Move the cursor up one line.</dd>
  718. <dt class=command id=command_goLineDown><code><strong>goLineDown</strong></code><span class=keybinding>Down, Ctrl-N (Mac)</span></dt>
  719. <dd>Move down one line.</dd>
  720. <dt class=command id=command_goPageUp><code><strong>goPageUp</strong></code><span class=keybinding>PageUp, Shift-Ctrl-V (Mac)</span></dt>
  721. <dd>Move the cursor up one screen, and scroll up by the same distance.</dd>
  722. <dt class=command id=command_goPageDown><code><strong>goPageDown</strong></code><span class=keybinding>PageDown, Ctrl-V (Mac)</span></dt>
  723. <dd>Move the cursor down one screen, and scroll down by the same distance.</dd>
  724. <dt class=command id=command_goCharLeft><code><strong>goCharLeft</strong></code><span class=keybinding>Left, Ctrl-B (Mac)</span></dt>
  725. <dd>Move the cursor one character left, going to the previous line
  726. when hitting the start of line.</dd>
  727. <dt class=command id=command_goCharRight><code><strong>goCharRight</strong></code><span class=keybinding>Right, Ctrl-F (Mac)</span></dt>
  728. <dd>Move the cursor one character right, going to the next line
  729. when hitting the end of line.</dd>
  730. <dt class=command id=command_goColumnLeft><code><strong>goColumnLeft</strong></code></dt>
  731. <dd>Move the cursor one character left, but don't cross line boundaries.</dd>
  732. <dt class=command id=command_goColumnRight><code><strong>goColumnRight</strong></code></dt>
  733. <dd>Move the cursor one character right, don't cross line boundaries.</dd>
  734. <dt class=command id=command_goWordLeft><code><strong>goWordLeft</strong></code><span class=keybinding>Alt-B (Mac)</span></dt>
  735. <dd>Move the cursor to the start of the previous word.</dd>
  736. <dt class=command id=command_goWordRight><code><strong>goWordRight</strong></code><span class=keybinding>Alt-F (Mac)</span></dt>
  737. <dd>Move the cursor to the end of the next word.</dd>
  738. <dt class=command id=command_goGroupLeft><code><strong>goGroupLeft</strong></code><span class=keybinding>Ctrl-Left (PC), Alt-Left (Mac)</span></dt>
  739. <dd>Move to the left of the group before the cursor. A group is
  740. a stretch of word characters, a stretch of punctuation
  741. characters, a newline, or a stretch of <em>more than one</em>
  742. whitespace character.</dd>
  743. <dt class=command id=command_goGroupRight><code><strong>goGroupRight</strong></code><span class=keybinding>Ctrl-Right (PC), Alt-Right (Mac)</span></dt>
  744. <dd>Move to the right of the group after the cursor
  745. (see <a href="#command_goGroupLeft">above</a>).</dd>
  746. <dt class=command id=command_delCharBefore><code><strong>delCharBefore</strong></code><span class=keybinding>Shift-Backspace, Ctrl-H (Mac)</span></dt>
  747. <dd>Delete the character before the cursor.</dd>
  748. <dt class=command id=command_delCharAfter><code><strong>delCharAfter</strong></code><span class=keybinding>Delete, Ctrl-D (Mac)</span></dt>
  749. <dd>Delete the character after the cursor.</dd>
  750. <dt class=command id=command_delWordBefore><code><strong>delWordBefore</strong></code><span class=keybinding>Alt-Backspace (Mac)</span></dt>
  751. <dd>Delete up to the start of the word before the cursor.</dd>
  752. <dt class=command id=command_delWordAfter><code><strong>delWordAfter</strong></code><span class=keybinding>Alt-D (Mac)</span></dt>
  753. <dd>Delete up to the end of the word after the cursor.</dd>
  754. <dt class=command id=command_delGroupBefore><code><strong>delGroupBefore</strong></code><span class=keybinding>Ctrl-Backspace (PC), Alt-Backspace (Mac)</span></dt>
  755. <dd>Delete to the left of the <a href="#command_goGroupLeft">group</a> before the cursor.</dd>
  756. <dt class=command id=command_delGroupAfter><code><strong>delGroupAfter</strong></code><span class=keybinding>Ctrl-Delete (PC), Ctrl-Alt-Backspace (Mac), Alt-Delete (Mac)</span></dt>
  757. <dd>Delete to the start of the <a href="#command_goGroupLeft">group</a> after the cursor.</dd>
  758. <dt class=command id=command_indentAuto><code><strong>indentAuto</strong></code><span class=keybinding>Shift-Tab</span></dt>
  759. <dd>Auto-indent the current line or selection.</dd>
  760. <dt class=command id=command_indentMore><code><strong>indentMore</strong></code><span class=keybinding>Ctrl-] (PC), Cmd-] (Mac)</span></dt>
  761. <dd>Indent the current line or selection by one <a href="#option_indentUnit">indent unit</a>.</dd>
  762. <dt class=command id=command_indentLess><code><strong>indentLess</strong></code><span class=keybinding>Ctrl-[ (PC), Cmd-[ (Mac)</span></dt>
  763. <dd>Dedent the current line or selection by one <a href="#option_indentUnit">indent unit</a>.</dd>
  764. <dt class=command id=command_insertTab><code><strong>insertTab</strong></code></dt>
  765. <dd>Insert a tab character at the cursor.</dd>
  766. <dt class=command id=command_insertSoftTab><code><strong>insertSoftTab</strong></code></dt>
  767. <dd>Insert the amount of spaces that match the width a tab at
  768. the cursor position would have.</dd>
  769. <dt class=command id=command_defaultTab><code><strong>defaultTab</strong></code><span class=keybinding>Tab</span></dt>
  770. <dd>If something is selected, indent it by
  771. one <a href="#option_indentUnit">indent unit</a>. If nothing is
  772. selected, insert a tab character.</dd>
  773. <dt class=command id=command_transposeChars><code><strong>transposeChars</strong></code><span class=keybinding>Ctrl-T (Mac)</span></dt>
  774. <dd>Swap the characters before and after the cursor.</dd>
  775. <dt class=command id=command_newlineAndIndent><code><strong>newlineAndIndent</strong></code><span class=keybinding>Enter</span></dt>
  776. <dd>Insert a newline and auto-indent the new line.</dd>
  777. <dt class=command id=command_toggleOverwrite><code><strong>toggleOverwrite</strong></code><span class=keybinding>Insert</span></dt>
  778. <dd>Flip the <a href="#toggleOverwrite">overwrite</a> flag.</dd>
  779. <dt class=command id=command_save><code><strong>save</strong></code><span class=keybinding>Ctrl-S (PC), Cmd-S (Mac)</span></dt>
  780. <dd>Not defined by the core library, only referred to in
  781. key maps. Intended to provide an easy way for user code to define
  782. a save command.</dd>
  783. <dt class=command id=command_find><code><strong>find</strong></code><span class=keybinding>Ctrl-F (PC), Cmd-F (Mac)</span></dt>
  784. <dt class=command id=command_findNext><code><strong>findNext</strong></code><span class=keybinding>Ctrl-G (PC), Cmd-G (Mac)</span></dt>
  785. <dt class=command id=command_findPrev><code><strong>findPrev</strong></code><span class=keybinding>Shift-Ctrl-G (PC), Shift-Cmd-G (Mac)</span></dt>
  786. <dt class=command id=command_replace><code><strong>replace</strong></code><span class=keybinding>Shift-Ctrl-F (PC), Cmd-Alt-F (Mac)</span></dt>
  787. <dt class=command id=command_replaceAll><code><strong>replaceAll</strong></code><span class=keybinding>Shift-Ctrl-R (PC), Shift-Cmd-Alt-F (Mac)</span></dt>
  788. <dd>Not defined by the core library, but defined in
  789. the <a href="#addon_search">search addon</a> (or custom client
  790. addons).</dd>
  791. </dl>
  792. </section>
  793. <section id=styling>
  794. <h2>Customized Styling</h2>
  795. <p>Up to a certain extent, CodeMirror's look can be changed by
  796. modifying style sheet files. The style sheets supplied by modes
  797. simply provide the colors for that mode, and can be adapted in a
  798. very straightforward way. To style the editor itself, it is
  799. possible to alter or override the styles defined
  800. in <a href="../lib/codemirror.css"><code>codemirror.css</code></a>.</p>
  801. <p>Some care must be taken there, since a lot of the rules in this
  802. file are necessary to have CodeMirror function properly. Adjusting
  803. colors should be safe, of course, and with some care a lot of
  804. other things can be changed as well. The CSS classes defined in
  805. this file serve the following roles:</p>
  806. <dl>
  807. <dt id="class_CodeMirror"><code><strong>CodeMirror</strong></code></dt>
  808. <dd>The outer element of the editor. This should be used for the
  809. editor width, height, borders and positioning. Can also be used
  810. to set styles that should hold for everything inside the editor
  811. (such as font and font size), or to set a background.</dd>
  812. <dt id="class_CodeMirror_scroll"><code><strong>CodeMirror-scroll</strong></code></dt>
  813. <dd>Whether the editor scrolls (<code>overflow: auto</code> +
  814. fixed height). By default, it does. Setting
  815. the <code>CodeMirror</code> class to have <code>height:
  816. auto</code> and giving this class <code>overflow-x: auto;
  817. overflow-y: hidden;</code> will cause the editor
  818. to <a href="../demo/resize.html">resize to fit its
  819. content</a>.</dd>
  820. <dt id="class_CodeMirror_focused"><code><strong>CodeMirror-focused</strong></code></dt>
  821. <dd>Whenever the editor is focused, the top element gets this
  822. class. This is used to hide the cursor and give the selection a
  823. different color when the editor is not focused.</dd>
  824. <dt id="class_CodeMirror_gutters"><code><strong>CodeMirror-gutters</strong></code></dt>
  825. <dd>This is the backdrop for all gutters. Use it to set the
  826. default gutter background color, and optionally add a border on
  827. the right of the gutters.</dd>
  828. <dt id="class_CodeMirror_linenumbers"><code><strong>CodeMirror-linenumbers</strong></code></dt>
  829. <dd>Use this for giving a background or width to the line number
  830. gutter.</dd>
  831. <dt id="class_CodeMirror_linenumber"><code><strong>CodeMirror-linenumber</strong></code></dt>
  832. <dd>Used to style the actual individual line numbers. These
  833. won't be children of the <code>CodeMirror-linenumbers</code>
  834. (plural) element, but rather will be absolutely positioned to
  835. overlay it. Use this to set alignment and text properties for
  836. the line numbers.</dd>
  837. <dt id="class_CodeMirror_lines"><code><strong>CodeMirror-lines</strong></code></dt>
  838. <dd>The visible lines. This is where you specify vertical
  839. padding for the editor content.</dd>
  840. <dt id="class_CodeMirror_cursor"><code><strong>CodeMirror-cursor</strong></code></dt>
  841. <dd>The cursor is a block element that is absolutely positioned.
  842. You can make it look whichever way you want.</dd>
  843. <dt id="class_CodeMirror_selected"><code><strong>CodeMirror-selected</strong></code></dt>
  844. <dd>The selection is represented by <code>span</code> elements
  845. with this class.</dd>
  846. <dt id="class_CodeMirror_matchingbracket"><code><strong>CodeMirror-matchingbracket</strong></code>,
  847. <code><strong>CodeMirror-nonmatchingbracket</strong></code></dt>
  848. <dd>These are used to style matched (or unmatched) brackets.</dd>
  849. </dl>
  850. <p>If your page's style sheets do funky things to
  851. all <code>div</code> or <code>pre</code> elements (you probably
  852. shouldn't do that), you'll have to define rules to cancel these
  853. effects out again for elements under the <code>CodeMirror</code>
  854. class.</p>
  855. <p>Themes are also simply CSS files, which define colors for
  856. various syntactic elements. See the files in
  857. the <a href="../theme/"><code>theme</code></a> directory.</p>
  858. </section>
  859. <section id=api>
  860. <h2>Programming API</h2>
  861. <p>A lot of CodeMirror features are only available through its
  862. API. Thus, you need to write code (or
  863. use <a href="#addons">addons</a>) if you want to expose them to
  864. your users.</p>
  865. <p>Whenever points in the document are represented, the API uses
  866. objects with <code>line</code> and <code>ch</code> properties.
  867. Both are zero-based. CodeMirror makes sure to 'clip' any positions
  868. passed by client code so that they fit inside the document, so you
  869. shouldn't worry too much about sanitizing your coordinates. If you
  870. give <code>ch</code> a value of <code>null</code>, or don't
  871. specify it, it will be replaced with the length of the specified
  872. line.</p>
  873. <p>Methods prefixed with <code>doc.</code> can, unless otherwise
  874. specified, be called both on <code>CodeMirror</code> (editor)
  875. instances and <code>CodeMirror.Doc</code> instances. Methods
  876. prefixed with <code>cm.</code> are <em>only</em> available
  877. on <code>CodeMirror</code> instances.</p>
  878. <h3 id="api_constructor">Constructor</h3>
  879. <p id="CodeMirror">Constructing an editor instance is done with
  880. the <code><strong>CodeMirror</strong>(place: Element|fn(Element),
  881. ?option: object)</code> constructor. If the <code>place</code>
  882. argument is a DOM element, the editor will be appended to it. If
  883. it is a function, it will be called, and is expected to place the
  884. editor into the document. <code>options</code> may be an element
  885. mapping <a href="#config">option names</a> to values. The options
  886. that it doesn't explicitly specify (or all options, if it is not
  887. passed) will be taken
  888. from <a href="#defaults"><code>CodeMirror.defaults</code></a>.</p>
  889. <p>Note that the options object passed to the constructor will be
  890. mutated when the instance's options
  891. are <a href="#setOption">changed</a>, so you shouldn't share such
  892. objects between instances.</p>
  893. <p>See <a href="#fromTextArea"><code>CodeMirror.fromTextArea</code></a>
  894. for another way to construct an editor instance.</p>
  895. <h3 id="api_content">Content manipulation methods</h3>
  896. <dl>
  897. <dt id="getValue"><code><strong>doc.getValue</strong>(?separator: string) → string</code></dt>
  898. <dd>Get the current editor content. You can pass it an optional
  899. argument to specify the string to be used to separate lines
  900. (defaults to <code>"\n"</code>).</dd>
  901. <dt id="setValue"><code><strong>doc.setValue</strong>(content: string)</code></dt>
  902. <dd>Set the editor content.</dd>
  903. <dt id="getRange"><code><strong>doc.getRange</strong>(from: {line, ch}, to: {line, ch}, ?separator: string) → string</code></dt>
  904. <dd>Get the text between the given points in the editor, which
  905. should be <code>{line, ch}</code> objects. An optional third
  906. argument can be given to indicate the line separator string to
  907. use (defaults to <code>"\n"</code>).</dd>
  908. <dt id="replaceRange"><code><strong>doc.replaceRange</strong>(replacement: string, from: {line, ch}, to: {line, ch}, ?origin: string)</code></dt>
  909. <dd>Replace the part of the document between <code>from</code>
  910. and <code>to</code> with the given string. <code>from</code>
  911. and <code>to</code> must be <code>{line, ch}</code>
  912. objects. <code>to</code> can be left off to simply insert the
  913. string at position <code>from</code>. When <code>origin</code>
  914. is given, it will be passed on
  915. to <a href="#event_change"><code>"change"</code> events</a>, and
  916. its first letter will be used to determine whether this change
  917. can be merged with previous history events, in the way described
  918. for <a href="#selection_origin">selection origins</a>.</dd>
  919. <dt id="getLine"><code><strong>doc.getLine</strong>(n: integer) → string</code></dt>
  920. <dd>Get the content of line <code>n</code>.</dd>
  921. <dt id="lineCount"><code><strong>doc.lineCount</strong>() → integer</code></dt>
  922. <dd>Get the number of lines in the editor.</dd>
  923. <dt id="firstLine"><code><strong>doc.firstLine</strong>() → integer</code></dt>
  924. <dd>Get the first line of the editor. This will
  925. usually be zero but for <a href="#linkedDoc_from">linked sub-views</a>,
  926. or <a href="#api_doc">documents</a> instantiated with a non-zero
  927. first line, it might return other values.</dd>
  928. <dt id="lastLine"><code><strong>doc.lastLine</strong>() → integer</code></dt>
  929. <dd>Get the last line of the editor. This will
  930. usually be <code>doc.lineCount() - 1</code>,
  931. but for <a href="#linkedDoc_from">linked sub-views</a>,
  932. it might return other values.</dd>
  933. <dt id="getLineHandle"><code><strong>doc.getLineHandle</strong>(num: integer) → LineHandle</code></dt>
  934. <dd>Fetches the line handle for the given line number.</dd>
  935. <dt id="getLineNumber"><code><strong>doc.getLineNumber</strong>(handle: LineHandle) → integer</code></dt>
  936. <dd>Given a line handle, returns the current position of that
  937. line (or <code>null</code> when it is no longer in the
  938. document).</dd>
  939. <dt id="eachLine"><code><strong>doc.eachLine</strong>(f: (line: LineHandle))</code></dt>
  940. <dt><code><strong>doc.eachLine</strong>(start: integer, end: integer, f: (line: LineHandle))</code></dt>
  941. <dd>Iterate over the whole document, or if <code>start</code>
  942. and <code>end</code> line numbers are given, the range
  943. from <code>start</code> up to (not including) <code>end</code>,
  944. and call <code>f</code> for each line, passing the line handle.
  945. This is a faster way to visit a range of line handlers than
  946. calling <a href="#getLineHandle"><code>getLineHandle</code></a>
  947. for each of them. Note that line handles have
  948. a <code>text</code> property containing the line's content (as a
  949. string).</dd>
  950. <dt id="markClean"><code><strong>doc.markClean</strong>()</code></dt>
  951. <dd>Set the editor content as 'clean', a flag that it will
  952. retain until it is edited, and which will be set again when such
  953. an edit is undone again. Useful to track whether the content
  954. needs to be saved. This function is deprecated in favor
  955. of <a href="#changeGeneration"><code>changeGeneration</code></a>,
  956. which allows multiple subsystems to track different notions of
  957. cleanness without interfering.</dd>
  958. <dt id="changeGeneration"><code><strong>doc.changeGeneration</strong>(?closeEvent: boolean) → integer</code></dt>
  959. <dd>Returns a number that can later be passed
  960. to <a href="#isClean"><code>isClean</code></a> to test whether
  961. any edits were made (and not undone) in the meantime.
  962. If <code>closeEvent</code> is true, the current history event
  963. will be ‘closed’, meaning it can't be combined with further
  964. changes (rapid typing or deleting events are typically
  965. combined).</dd>
  966. <dt id="isClean"><code><strong>doc.isClean</strong>(?generation: integer) → boolean</code></dt>
  967. <dd>Returns whether the document is currently clean — not
  968. modified since initialization or the last call
  969. to <a href="#markClean"><code>markClean</code></a> if no
  970. argument is passed, or since the matching call
  971. to <a href="#changeGeneration"><code>changeGeneration</code></a>
  972. if a generation value is given.</dd>
  973. </dl>
  974. <h3 id="api_selection">Cursor and selection methods</h3>
  975. <dl>
  976. <dt id="getSelection"><code><strong>doc.getSelection</strong>(?lineSep: string) → string</code></dt>
  977. <dd>Get the currently selected code. Optionally pass a line
  978. separator to put between the lines in the output. When multiple
  979. selections are present, they are concatenated with instances
  980. of <code>lineSep</code> in between.</dd>
  981. <dt id="getSelections"><code><strong>doc.getSelections</strong>(?lineSep: string) → string</code></dt>
  982. <dd>Returns an array containing a string for each selection,
  983. representing the content of the selections.</dd>
  984. <dt id="replaceSelection"><code><strong>doc.replaceSelection</strong>(replacement: string, ?select: string)</code></dt>
  985. <dd>Replace the selection(s) with the given string. By default,
  986. the new selection ends up after the inserted text. The
  987. optional <code>select</code> argument can be used to change
  988. this—passing <code>"around"</code> will cause the new text to be
  989. selected, passing <code>"start"</code> will collapse the
  990. selection to the start of the inserted text.</dd>
  991. <dt id="replaceSelections"><code><strong>doc.replaceSelections</strong>(replacements: array&lt;string&gt;, ?select: string)</code></dt>
  992. <dd>The length of the given array should be the same as the
  993. number of active selections. Replaces the content of the
  994. selections with the strings in the array.
  995. The <code>select</code> argument works the same as
  996. in <a href="#replaceSelection"><code>replaceSelection</code></a>.</dd>
  997. <dt id="getCursor"><code><strong>doc.getCursor</strong>(?start: string) → {line, ch}</code></dt>
  998. <dd>Retrieve one end of the <em>primary</em>
  999. selection. <code>start</code> is a an optional string indicating
  1000. which end of the selection to return. It may
  1001. be <code>"from"</code>, <code>"to"</code>, <code>"head"</code>
  1002. (the side of the selection that moves when you press
  1003. shift+arrow), or <code>"anchor"</code> (the fixed side of the
  1004. selection). Omitting the argument is the same as
  1005. passing <code>"head"</code>. A <code>{line, ch}</code> object
  1006. will be returned.</dd>
  1007. <dt id="listSelections"><code><strong>doc.listSelections</strong>() → array&lt;{anchor, head}&lt;</code></dt>
  1008. <dd>Retrieves a list of all current selections. These will
  1009. always be sorted, and never overlap (overlapping selections are
  1010. merged). Each object in the array contains <code>anchor</code>
  1011. and <code>head</code> properties referring to <code>{line,
  1012. ch}</code> objects.</dd>
  1013. <dt id="somethingSelected"><code><strong>doc.somethingSelected</strong>() → boolean</code></dt>
  1014. <dd>Return true if any text is selected.</dd>
  1015. <dt id="setCursor"><code><strong>doc.setCursor</strong>(pos: {line, ch}|number, ?ch: number, ?options: object)</code></dt>
  1016. <dd>Set the cursor position. You can either pass a
  1017. single <code>{line, ch}</code> object, or the line and the
  1018. character as two separate parameters. Will replace all
  1019. selections with a single, empty selection at the given position.
  1020. The supported options are the same as for <a href="#setSelection"><code>setSelection</code></a>.</dd>
  1021. <dt id="setSelection"><code><strong>doc.setSelection</strong>(anchor: {line, ch}, ?head: {line, ch}, ?options: object)</code></dt>
  1022. <dd>Set a single selection range. <code>anchor</code>
  1023. and <code>head</code> should be <code>{line, ch}</code>
  1024. objects. <code>head</code> defaults to <code>anchor</code> when
  1025. not given. These options are supported:
  1026. <dl>
  1027. <dt id="selection_scroll"><code><strong>scroll</strong>: boolean</code></dt>
  1028. <dd>Determines whether the selection head should be scrolled
  1029. into view. Defaults to true.</dd>
  1030. <dt id="selection_origin"><code><strong>origin</strong>: string</code></dt>
  1031. <dd>Detemines whether the selection history event may be
  1032. merged with the previous one. When an origin starts with the
  1033. character <code>+</code>, and the last recorded selection had
  1034. the same origin and was similar (close
  1035. in <a href="#option_historyEventDelay">time</a>, both
  1036. collapsed or both non-collapsed), the new one will replace the
  1037. old one. When it starts with <code>*</code>, it will always
  1038. replace the previous event (if that had the same origin).
  1039. Built-in motion uses the <code>"+move"</code> origin.</dd>
  1040. <dt id="selection_bias"><code><strong>bias</strong>: number</code></dt>
  1041. <dd>Determine the direction into which the selection endpoints
  1042. should be adjusted when they fall inside
  1043. an <a href="#mark_atomic">atomic</a> range. Can be either -1
  1044. (backward) or 1 (forward). When not given, the bias will be
  1045. based on the relative position of the old selection—the editor
  1046. will try to move further away from that, to prevent getting
  1047. stuck.</dd>
  1048. </dl></dd>
  1049. <dt id="setSelections"><code><strong>doc.setSelections</strong>(ranges: array&lt;{anchor, head}&gt;, ?primary: integer, ?options: object)</code></dt>
  1050. <dd>Sets a new set of selections. There must be at least one
  1051. selection in the given array. When <code>primary</code> is a
  1052. number, it determines which selection is the primary one. When
  1053. it is not given, the primary index is taken from the previous
  1054. selection, or set to the last range if the previous selection
  1055. had less ranges than the new one. Supports the same options
  1056. as <a href="#setSelection"><code>setSelection</code></a>.</dd>
  1057. <dt id="addSelection"><code><strong>doc.addSelection</strong>(anchor: {line, ch}, ?head: {line, ch})</code></dt>
  1058. <dd>Adds a new selection to the existing set of selections, and
  1059. makes it the primary selection.</dd>
  1060. <dt id="extendSelection"><code><strong>doc.extendSelection</strong>(from: {line, ch}, ?to: {line, ch}, ?options: object)</code></dt>
  1061. <dd>Similar
  1062. to <a href="#setSelection"><code>setSelection</code></a>, but
  1063. will, if shift is held or
  1064. the <a href="#setExtending">extending</a> flag is set, move the
  1065. head of the selection while leaving the anchor at its current
  1066. place. <code>to</code> is optional, and can be passed to ensure
  1067. a region (for example a word or paragraph) will end up selected
  1068. (in addition to whatever lies between that region and the
  1069. current anchor). When multiple selections are present, all but
  1070. the primary selection will be dropped by this method.
  1071. Supports the same options as <a href="#setSelection"><code>setSelection</code></a>.</dd>
  1072. <dt id="extendSelections"><code><strong>doc.extendSelections</strong>(heads: array&lt;{line, ch}&gt;, ?options: object)</code></dt>
  1073. <dd>An equivalent
  1074. of <a href="#extendSelection"><code>extendSelection</code></a>
  1075. that acts on all selections at once.</dd>
  1076. <dt id="extendSelectionsBy"><code><strong>doc.extendSelectionsBy</strong>(f: function(range: {anchor, head}) → {anchor, head}), ?options: object)</code></dt>
  1077. <dd>Applies the given function to all existing selections, and
  1078. calls <a href="#extendSelections"><code>extendSelections</code></a>
  1079. on the result.</dd>
  1080. <dt id="setExtending"><code><strong>doc.setExtending</strong>(value: boolean)</code></dt>
  1081. <dd>Sets or clears the 'extending' flag, which acts similar to
  1082. the shift key, in that it will cause cursor movement and calls
  1083. to <a href="#extendSelection"><code>extendSelection</code></a>
  1084. to leave the selection anchor in place.</dd>
  1085. <dt id="getExtending"><code><strong>doc.getExtending</strong>() → boolean</code></dt>
  1086. <dd>Get the value of the 'extending' flag.</dd>
  1087. <dt id="hasFocus"><code><strong>cm.hasFocus</strong>() → boolean</code></dt>
  1088. <dd>Tells you whether the editor currently has focus.</dd>
  1089. <dt id="findPosH"><code><strong>cm.findPosH</strong>(start: {line, ch}, amount: integer, unit: string, visually: boolean) → {line, ch, ?hitSide: boolean}</code></dt>
  1090. <dd>Used to find the target position for horizontal cursor
  1091. motion. <code>start</code> is a <code>{line, ch}</code>
  1092. object, <code>amount</code> an integer (may be negative),
  1093. and <code>unit</code> one of the
  1094. string <code>"char"</code>, <code>"column"</code>,
  1095. or <code>"word"</code>. Will return a position that is produced
  1096. by moving <code>amount</code> times the distance specified
  1097. by <code>unit</code>. When <code>visually</code> is true, motion
  1098. in right-to-left text will be visual rather than logical. When
  1099. the motion was clipped by hitting the end or start of the
  1100. document, the returned value will have a <code>hitSide</code>
  1101. property set to true.</dd>
  1102. <dt id="findPosV"><code><strong>cm.findPosV</strong>(start: {line, ch}, amount: integer, unit: string) → {line, ch, ?hitSide: boolean}</code></dt>
  1103. <dd>Similar to <a href="#findPosH"><code>findPosH</code></a>,
  1104. but used for vertical motion. <code>unit</code> may
  1105. be <code>"line"</code> or <code>"page"</code>. The other
  1106. arguments and the returned value have the same interpretation as
  1107. they have in <code>findPosH</code>.</dd>
  1108. <dt id="findWordAt"><code><strong>cm.findWordAt</strong>(pos: {line, ch}) → {anchor: {line, ch}, head: {line, ch}}</code></dt>
  1109. <dd>Returns the start and end of the 'word' (the stretch of
  1110. letters, whitespace, or punctuation) at the given position.</dd>
  1111. </dl>
  1112. <h3 id="api_configuration">Configuration methods</h3>
  1113. <dl>
  1114. <dt id="setOption"><code><strong>cm.setOption</strong>(option: string, value: any)</code></dt>
  1115. <dd>Change the configuration of the editor. <code>option</code>
  1116. should the name of an <a href="#config">option</a>,
  1117. and <code>value</code> should be a valid value for that
  1118. option.</dd>
  1119. <dt id="getOption"><code><strong>cm.getOption</strong>(option: string) → any</code></dt>
  1120. <dd>Retrieves the current value of the given option for this
  1121. editor instance.</dd>
  1122. <dt id="addKeyMap"><code><strong>cm.addKeyMap</strong>(map: object, bottom: boolean)</code></dt>
  1123. <dd>Attach an additional <a href="#keymaps">key map</a> to the
  1124. editor. This is mostly useful for addons that need to register
  1125. some key handlers without trampling on
  1126. the <a href="#option_extraKeys"><code>extraKeys</code></a>
  1127. option. Maps added in this way have a higher precedence than
  1128. the <code>extraKeys</code>
  1129. and <a href="#option_keyMap"><code>keyMap</code></a> options,
  1130. and between them, the maps added earlier have a lower precedence
  1131. than those added later, unless the <code>bottom</code> argument
  1132. was passed, in which case they end up below other key maps added
  1133. with this method.</dd>
  1134. <dt id="removeKeyMap"><code><strong>cm.removeKeyMap</strong>(map: object)</code></dt>
  1135. <dd>Disable a keymap added
  1136. with <a href="#addKeyMap"><code>addKeyMap</code></a>. Either
  1137. pass in the key map object itself, or a string, which will be
  1138. compared against the <code>name</code> property of the active
  1139. key maps.</dd>
  1140. <dt id="addOverlay"><code><strong>cm.addOverlay</strong>(mode: string|object, ?options: object)</code></dt>
  1141. <dd>Enable a highlighting overlay. This is a stateless mini-mode
  1142. that can be used to add extra highlighting. For example,
  1143. the <a href="../demo/search.html">search addon</a> uses it to
  1144. highlight the term that's currently being
  1145. searched. <code>mode</code> can be a <a href="#option_mode">mode
  1146. spec</a> or a mode object (an object with
  1147. a <a href="#token"><code>token</code></a> method).
  1148. The <code>options</code> parameter is optional. If given, it
  1149. should be an object. Currently, only the <code>opaque</code>
  1150. option is recognized. This defaults to off, but can be given to
  1151. allow the overlay styling, when not <code>null</code>, to
  1152. override the styling of the base mode entirely, instead of the
  1153. two being applied together.</dd>
  1154. <dt id="removeOverlay"><code><strong>cm.removeOverlay</strong>(mode: string|object)</code></dt>
  1155. <dd>Pass this the exact value passed for the <code>mode</code>
  1156. parameter to <a href="#addOverlay"><code>addOverlay</code></a>,
  1157. or a string that corresponds to the <code>name</code> propery of
  1158. that value, to remove an overlay again.</dd>
  1159. <dt id="on"><code><strong>cm.on</strong>(type: string, func: (...args))</code></dt>
  1160. <dd>Register an event handler for the given event type (a
  1161. string) on the editor instance. There is also
  1162. a <code>CodeMirror.on(object, type, func)</code> version
  1163. that allows registering of events on any object.</dd>
  1164. <dt id="off"><code><strong>cm.off</strong>(type: string, func: (...args))</code></dt>
  1165. <dd>Remove an event handler on the editor instance. An
  1166. equivalent <code>CodeMirror.off(object, type,
  1167. func)</code> also exists.</dd>
  1168. </dl>
  1169. <h3 id="api_doc">Document management methods</h3>
  1170. <p id="Doc">Each editor is associated with an instance
  1171. of <code>CodeMirror.Doc</code>, its document. A document
  1172. represents the editor content, plus a selection, an undo history,
  1173. and a <a href="#option_mode">mode</a>. A document can only be
  1174. associated with a single editor at a time. You can create new
  1175. documents by calling the <code>CodeMirror.Doc(text, mode,
  1176. firstLineNumber)</code> constructor. The last two arguments are
  1177. optional and can be used to set a mode for the document and make
  1178. it start at a line number other than 0, respectively.</p>
  1179. <dl>
  1180. <dt id="getDoc"><code><strong>cm.getDoc</strong>() → Doc</code></dt>
  1181. <dd>Retrieve the currently active document from an editor.</dd>
  1182. <dt id="getEditor"><code><strong>doc.getEditor</strong>() → CodeMirror</code></dt>
  1183. <dd>Retrieve the editor associated with a document. May
  1184. return <code>null</code>.</dd>
  1185. <dt id="swapDoc"><code><strong>cm.swapDoc</strong>(doc: CodeMirror.Doc) → Doc</code></dt>
  1186. <dd>Attach a new document to the editor. Returns the old
  1187. document, which is now no longer associated with an editor.</dd>
  1188. <dt id="copy"><code><strong>doc.copy</strong>(copyHistory: boolean) → Doc</code></dt>
  1189. <dd>Create an identical copy of the given doc.
  1190. When <code>copyHistory</code> is true, the history will also be
  1191. copied. Can not be called directly on an editor.</dd>
  1192. <dt id="linkedDoc"><code><strong>doc.linkedDoc</strong>(options: object) → Doc</code></dt>
  1193. <dd>Create a new document that's linked to the target document.
  1194. Linked documents will stay in sync (changes to one are also
  1195. applied to the other) until <a href="#unlinkDoc">unlinked</a>.
  1196. These are the options that are supported:
  1197. <dl>
  1198. <dt id="linkedDoc_sharedHist"><code><strong>sharedHist</strong>: boolean</code></dt>
  1199. <dd>When turned on, the linked copy will share an undo
  1200. history with the original. Thus, something done in one of
  1201. the two can be undone in the other, and vice versa.</dd>
  1202. <dt id="linkedDoc_from"><code><strong>from</strong>: integer</code></dt>
  1203. <dt id="linkedDoc_to"><code><strong>to</strong>: integer</code></dt>
  1204. <dd>Can be given to make the new document a subview of the
  1205. original. Subviews only show a given range of lines. Note
  1206. that line coordinates inside the subview will be consistent
  1207. with those of the parent, so that for example a subview
  1208. starting at line 10 will refer to its first line as line 10,
  1209. not 0.</dd>
  1210. <dt id="linkedDoc_mode"><code><strong>mode</strong>: string|object</code></dt>
  1211. <dd>By default, the new document inherits the mode of the
  1212. parent. This option can be set to
  1213. a <a href="#option_mode">mode spec</a> to give it a
  1214. different mode.</dd>
  1215. </dl></dd>
  1216. <dt id="unlinkDoc"><code><strong>doc.unlinkDoc</strong>(doc: CodeMirror.Doc)</code></dt>
  1217. <dd>Break the link between two documents. After calling this,
  1218. changes will no longer propagate between the documents, and, if
  1219. they had a shared history, the history will become
  1220. separate.</dd>
  1221. <dt id="iterLinkedDocs"><code><strong>doc.iterLinkedDocs</strong>(function: (doc: CodeMirror.Doc, sharedHist: boolean))</code></dt>
  1222. <dd>Will call the given function for all documents linked to the
  1223. target document. It will be passed two arguments, the linked document
  1224. and a boolean indicating whether that document shares history
  1225. with the target.</dd>
  1226. </dl>
  1227. <h3 id="api_history">History-related methods</h3>
  1228. <dl>
  1229. <dt id="undo"><code><strong>doc.undo</strong>()</code></dt>
  1230. <dd>Undo one edit (if any undo events are stored).</dd>
  1231. <dt id="redo"><code><strong>doc.redo</strong>()</code></dt>
  1232. <dd>Redo one undone edit.</dd>
  1233. <dt id="undoSelection"><code><strong>doc.undoSelection</strong>()</code></dt>
  1234. <dd>Undo one edit or selection change.</dd>
  1235. <dt id="redoSelection"><code><strong>doc.redoSelection</strong>()</code></dt>
  1236. <dd>Redo one undone edit or selection change.</dd>
  1237. <dt id="historySize"><code><strong>doc.historySize</strong>() → {undo: integer, redo: integer}</code></dt>
  1238. <dd>Returns an object with <code>{undo, redo}</code> properties,
  1239. both of which hold integers, indicating the amount of stored
  1240. undo and redo operations.</dd>
  1241. <dt id="clearHistory"><code><strong>doc.clearHistory</strong>()</code></dt>
  1242. <dd>Clears the editor's undo history.</dd>
  1243. <dt id="getHistory"><code><strong>doc.getHistory</strong>() → object</code></dt>
  1244. <dd>Get a (JSON-serializeable) representation of the undo history.</dd>
  1245. <dt id="setHistory"><code><strong>doc.setHistory</strong>(history: object)</code></dt>
  1246. <dd>Replace the editor's undo history with the one provided,
  1247. which must be a value as returned
  1248. by <a href="#getHistory"><code>getHistory</code></a>. Note that
  1249. this will have entirely undefined results if the editor content
  1250. isn't also the same as it was when <code>getHistory</code> was
  1251. called.</dd>
  1252. </dl>
  1253. <h3 id="api_marker">Text-marking methods</h3>
  1254. <dl>
  1255. <dt id="markText"><code><strong>doc.markText</strong>(from: {line, ch}, to: {line, ch}, ?options: object) → TextMarker</code></dt>
  1256. <dd>Can be used to mark a range of text with a specific CSS
  1257. class name. <code>from</code> and <code>to</code> should
  1258. be <code>{line, ch}</code> objects. The <code>options</code>
  1259. parameter is optional. When given, it should be an object that
  1260. may contain the following configuration options:
  1261. <dl>
  1262. <dt id="mark_className"><code><strong>className</strong>: string</code></dt>
  1263. <dd>Assigns a CSS class to the marked stretch of text.</dd>
  1264. <dt id="mark_inclusiveLeft"><code><strong>inclusiveLeft</strong>: boolean</code></dt>
  1265. <dd>Determines whether
  1266. text inserted on the left of the marker will end up inside
  1267. or outside of it.</dd>
  1268. <dt id="mark_inclusiveRight"><code><strong>inclusiveRight</strong>: boolean</code></dt>
  1269. <dd>Like <code>inclusiveLeft</code>,
  1270. but for the right side.</dd>
  1271. <dt id="mark_atomic"><code><strong>atomic</strong>: boolean</code></dt>
  1272. <dd>Atomic ranges act as a single unit when cursor movement is
  1273. concerned—i.e. it is impossible to place the cursor inside of
  1274. them. In atomic ranges, <code>inclusiveLeft</code>
  1275. and <code>inclusiveRight</code> have a different meaning—they
  1276. will prevent the cursor from being placed respectively
  1277. directly before and directly after the range.</dd>
  1278. <dt id="mark_collapsed"><code><strong>collapsed</strong>: boolean</code></dt>
  1279. <dd>Collapsed ranges do not show up in the display. Setting a
  1280. range to be collapsed will automatically make it atomic.</dd>
  1281. <dt id="mark_clearOnEnter"><code><strong>clearOnEnter</strong>: boolean</code></dt>
  1282. <dd>When enabled, will cause the mark to clear itself whenever
  1283. the cursor enters its range. This is mostly useful for
  1284. text-replacement widgets that need to 'snap open' when the
  1285. user tries to edit them. The
  1286. <a href="#event_clear"><code>"clear"</code></a> event
  1287. fired on the range handle can be used to be notified when this
  1288. happens.</dd>
  1289. <dt id="mark_clearWhenEmpty"><code><strong>clearWhenEmpty</strong>: boolean</code></dt>
  1290. <dd>Determines whether the mark is automatically cleared when
  1291. it becomes empty. Default is true.</dd>
  1292. <dt id="mark_replacedWith"><code><strong>replacedWith</strong>: Element</code></dt>
  1293. <dd>Use a given node to display this range. Implies both
  1294. collapsed and atomic. The given DOM node <em>must</em> be an
  1295. inline element (as opposed to a block element).</dd>
  1296. <dt><code><strong>handleMouseEvents</strong>: boolean</code></dt>
  1297. <dd>When <code>replacedWith</code> is given, this determines
  1298. whether the editor will capture mouse and drag events
  1299. occurring in this widget. Default is false—the events will be
  1300. left alone for the default browser handler, or specific
  1301. handlers on the widget, to capture.</dd>
  1302. <dt id="mark_readOnly"><code><strong>readOnly</strong>: boolean</code></dt>
  1303. <dd>A read-only span can, as long as it is not cleared, not be
  1304. modified except by
  1305. calling <a href="#setValue"><code>setValue</code></a> to reset
  1306. the whole document. <em>Note:</em> adding a read-only span
  1307. currently clears the undo history of the editor, because
  1308. existing undo events being partially nullified by read-only
  1309. spans would corrupt the history (in the current
  1310. implementation).</dd>
  1311. <dt id="mark_addToHistory"><code><strong>addToHistory</strong>: boolean</code></dt>
  1312. <dd>When set to true (default is false), adding this marker
  1313. will create an event in the undo history that can be
  1314. individually undone (clearing the marker).</dd>
  1315. <dt id="mark_startStyle"><code><strong>startStyle</strong>: string</code></dt><dd>Can be used to specify
  1316. an extra CSS class to be applied to the leftmost span that
  1317. is part of the marker.</dd>
  1318. <dt id="mark_endStyle"><code><strong>endStyle</strong>: string</code></dt><dd>Equivalent
  1319. to <code>startStyle</code>, but for the rightmost span.</dd>
  1320. <dt id="mark_title"><code><strong>title</strong>:
  1321. string</code></dt><dd>When given, will give the nodes created
  1322. for this span a HTML <code>title</code> attribute with the
  1323. given value.</dd>
  1324. <dt id="mark_shared"><code><strong>shared</strong>: boolean</code></dt><dd>When the
  1325. target document is <a href="#linkedDoc">linked</a> to other
  1326. documents, you can set <code>shared</code> to true to make the
  1327. marker appear in all documents. By default, a marker appears
  1328. only in its target document.</dd>
  1329. </dl>
  1330. The method will return an object that represents the marker
  1331. (with constructor <code>CodeMirror.TextMarker</code>), which
  1332. exposes three methods:
  1333. <code><strong>clear</strong>()</code>, to remove the mark,
  1334. <code><strong>find</strong>()</code>, which returns
  1335. a <code>{from, to}</code> object (both holding document
  1336. positions), indicating the current position of the marked range,
  1337. or <code>undefined</code> if the marker is no longer in the
  1338. document, and finally <code><strong>changed</strong>()</code>,
  1339. which you can call if you've done something that might change
  1340. the size of the marker (for example changing the content of
  1341. a <a href="#mark_replacedWith"><code>replacedWith</code></a>
  1342. node), and want to cheaply update the display.</dd>
  1343. <dt id="setBookmark"><code><strong>doc.setBookmark</strong>(pos: {line, ch}, ?options: object) → TextMarker</code></dt>
  1344. <dd>Inserts a bookmark, a handle that follows the text around it
  1345. as it is being edited, at the given position. A bookmark has two
  1346. methods <code>find()</code> and <code>clear()</code>. The first
  1347. returns the current position of the bookmark, if it is still in
  1348. the document, and the second explicitly removes the bookmark.
  1349. The options argument is optional. If given, the following
  1350. properties are recognized:
  1351. <dl>
  1352. <dt><code><strong>widget</strong>: Element</code></dt><dd>Can be used to display a DOM
  1353. node at the current location of the bookmark (analogous to
  1354. the <a href="#mark_replacedWith"><code>replacedWith</code></a>
  1355. option to <a href="#markText"><code>markText</code></a>).</dd>
  1356. <dt><code><strong>insertLeft</strong>: boolean</code></dt><dd>By default, text typed
  1357. when the cursor is on top of the bookmark will end up to the
  1358. right of the bookmark. Set this option to true to make it go
  1359. to the left instead.</dd>
  1360. <dt><code><strong>shared</strong>: boolean</code></dt><dd>See
  1361. the corresponding <a href="#mark_shared">option</a>
  1362. to <code>markText</code>.</dd>
  1363. </dl></dd>
  1364. <dt id="findMarks"><code><strong>doc.findMarks</strong>(from: {line, ch}, to: {line, ch}) → array&lt;TextMarker&gt;</code></dt>
  1365. <dd>Returns an array of all the bookmarks and marked ranges
  1366. found between the given positions.</dd>
  1367. <dt id="findMarksAt"><code><strong>doc.findMarksAt</strong>(pos: {line, ch}) → array&lt;TextMarker&gt;</code></dt>
  1368. <dd>Returns an array of all the bookmarks and marked ranges
  1369. present at the given position.</dd>
  1370. <dt id="getAllMarks"><code><strong>doc.getAllMarks</strong>() → array&lt;TextMarker&gt;</code></dt>
  1371. <dd>Returns an array containing all marked ranges in the document.</dd>
  1372. </dl>
  1373. <h3 id="api_decoration">Widget, gutter, and decoration methods</h3>
  1374. <dl>
  1375. <dt id="setGutterMarker"><code><strong>cm.setGutterMarker</strong>(line: integer|LineHandle, gutterID: string, value: Element) → LineHandle</code></dt>
  1376. <dd>Sets the gutter marker for the given gutter (identified by
  1377. its CSS class, see
  1378. the <a href="#option_gutters"><code>gutters</code></a> option)
  1379. to the given value. Value can be either <code>null</code>, to
  1380. clear the marker, or a DOM element, to set it. The DOM element
  1381. will be shown in the specified gutter next to the specified
  1382. line.</dd>
  1383. <dt id="clearGutter"><code><strong>cm.clearGutter</strong>(gutterID: string)</code></dt>
  1384. <dd>Remove all gutter markers in
  1385. the <a href="#option_gutters">gutter</a> with the given ID.</dd>
  1386. <dt id="addLineClass"><code><strong>doc.addLineClass</strong>(line: integer|LineHandle, where: string, class: string) → LineHandle</code></dt>
  1387. <dd>Set a CSS class name for the given line. <code>line</code>
  1388. can be a number or a line handle. <code>where</code> determines
  1389. to which element this class should be applied, can can be one
  1390. of <code>"text"</code> (the text element, which lies in front of
  1391. the selection), <code>"background"</code> (a background element
  1392. that will be behind the selection), or <code>"wrap"</code> (the
  1393. wrapper node that wraps all of the line's elements, including
  1394. gutter elements). <code>class</code> should be the name of the
  1395. class to apply.</dd>
  1396. <dt id="removeLineClass"><code><strong>doc.removeLineClass</strong>(line: integer|LineHandle, where: string, class: string) → LineHandle</code></dt>
  1397. <dd>Remove a CSS class from a line. <code>line</code> can be a
  1398. line handle or number. <code>where</code> should be one
  1399. of <code>"text"</code>, <code>"background"</code>,
  1400. or <code>"wrap"</code>
  1401. (see <a href="#addLineClass"><code>addLineClass</code></a>). <code>class</code>
  1402. can be left off to remove all classes for the specified node, or
  1403. be a string to remove only a specific class.</dd>
  1404. <dt id="lineInfo"><code><strong>cm.lineInfo</strong>(line: integer|LineHandle) → object</code></dt>
  1405. <dd>Returns the line number, text content, and marker status of
  1406. the given line, which can be either a number or a line handle.
  1407. The returned object has the structure <code>{line, handle, text,
  1408. gutterMarkers, textClass, bgClass, wrapClass, widgets}</code>,
  1409. where <code>gutterMarkers</code> is an object mapping gutter IDs
  1410. to marker elements, and <code>widgets</code> is an array
  1411. of <a href="#addLineWidget">line widgets</a> attached to this
  1412. line, and the various class properties refer to classes added
  1413. with <a href="#addLineClass"><code>addLineClass</code></a>.</dd>
  1414. <dt id="addWidget"><code><strong>cm.addWidget</strong>(pos: {line, ch}, node: Element, scrollIntoView: boolean)</code></dt>
  1415. <dd>Puts <code>node</code>, which should be an absolutely
  1416. positioned DOM node, into the editor, positioned right below the
  1417. given <code>{line, ch}</code> position.
  1418. When <code>scrollIntoView</code> is true, the editor will ensure
  1419. that the entire node is visible (if possible). To remove the
  1420. widget again, simply use DOM methods (move it somewhere else, or
  1421. call <code>removeChild</code> on its parent).</dd>
  1422. <dt id="addLineWidget"><code><strong>cm.addLineWidget</strong>(line: integer|LineHandle, node: Element, ?options: object) → LineWidget</code></dt>
  1423. <dd>Adds a line widget, an element shown below a line, spanning
  1424. the whole of the editor's width, and moving the lines below it
  1425. downwards. <code>line</code> should be either an integer or a
  1426. line handle, and <code>node</code> should be a DOM node, which
  1427. will be displayed below the given line. <code>options</code>,
  1428. when given, should be an object that configures the behavior of
  1429. the widget. The following options are supported (all default to
  1430. false):
  1431. <dl>
  1432. <dt><code><strong>coverGutter</strong>: boolean</code></dt>
  1433. <dd>Whether the widget should cover the gutter.</dd>
  1434. <dt><code><strong>noHScroll</strong>: boolean</code></dt>
  1435. <dd>Whether the widget should stay fixed in the face of
  1436. horizontal scrolling.</dd>
  1437. <dt><code><strong>above</strong>: boolean</code></dt>
  1438. <dd>Causes the widget to be placed above instead of below
  1439. the text of the line.</dd>
  1440. <dt><code><strong>handleMouseEvents</strong>: boolean</code></dt>
  1441. <dd>Determines whether the editor will capture mouse and
  1442. drag events occurring in this widget. Default is false—the
  1443. events will be left alone for the default browser handler,
  1444. or specific handlers on the widget, to capture.</dd>
  1445. <dt><code><strong>insertAt</strong>: integer</code></dt>
  1446. <dd>By default, the widget is added below other widgets for
  1447. the line. This option can be used to place it at a different
  1448. position (zero for the top, N to put it after the Nth other
  1449. widget). Note that this only has effect once, when the
  1450. widget is created.
  1451. </dl>
  1452. Note that the widget node will become a descendant of nodes with
  1453. CodeMirror-specific CSS classes, and those classes might in some
  1454. cases affect it. This method returns an object that represents
  1455. the widget placement. It'll have a <code>line</code> property
  1456. pointing at the line handle that it is associated with, and the following methods:
  1457. <dl>
  1458. <dt id="widget_clear"><code><strong>clear</strong>()</code></dt><dd>Removes the widget.</dd>
  1459. <dt id="widget_changed"><code><strong>changed</strong>()</code></dt><dd>Call
  1460. this if you made some change to the widget's DOM node that
  1461. might affect its height. It'll force CodeMirror to update
  1462. the height of the line that contains the widget.</dd>
  1463. </dl>
  1464. </dd>
  1465. </dl>
  1466. <h3 id="api_sizing">Sizing, scrolling and positioning methods</h3>
  1467. <dl>
  1468. <dt id="setSize"><code><strong>cm.setSize</strong>(width: number|string, height: number|string)</code></dt>
  1469. <dd>Programatically set the size of the editor (overriding the
  1470. applicable <a href="#css-resize">CSS
  1471. rules</a>). <code>width</code> and <code>height</code>
  1472. can be either numbers (interpreted as pixels) or CSS units
  1473. (<code>"100%"</code>, for example). You can
  1474. pass <code>null</code> for either of them to indicate that that
  1475. dimension should not be changed.</dd>
  1476. <dt id="scrollTo"><code><strong>cm.scrollTo</strong>(x: number, y: number)</code></dt>
  1477. <dd>Scroll the editor to a given (pixel) position. Both
  1478. arguments may be left as <code>null</code>
  1479. or <code>undefined</code> to have no effect.</dd>
  1480. <dt id="getScrollInfo"><code><strong>cm.getScrollInfo</strong>() → {left, top, width, height, clientWidth, clientHeight}</code></dt>
  1481. <dd>Get an <code>{left, top, width, height, clientWidth,
  1482. clientHeight}</code> object that represents the current scroll
  1483. position, the size of the scrollable area, and the size of the
  1484. visible area (minus scrollbars).</dd>
  1485. <dt id="scrollIntoView"><code><strong>cm.scrollIntoView</strong>(what: {line, ch}|{left, top, right, bottom}|{from, to}|null, ?margin: number)</code></dt>
  1486. <dd>Scrolls the given position into view. <code>what</code> may
  1487. be <code>null</code> to scroll the cursor into view,
  1488. a <code>{line, ch}</code> position to scroll a character into
  1489. view, a <code>{left, top, right, bottom}</code> pixel range (in
  1490. editor-local coordinates), or a range <code>{from, to}</code>
  1491. containing either two character positions or two pixel squares.
  1492. The <code>margin</code> parameter is optional. When given, it
  1493. indicates the amount of vertical pixels around the given area
  1494. that should be made visible as well.</dd>
  1495. <dt id="cursorCoords"><code><strong>cm.cursorCoords</strong>(where: boolean|{line, ch}, mode: string) → {left, top, bottom}</code></dt>
  1496. <dd>Returns an <code>{left, top, bottom}</code> object
  1497. containing the coordinates of the cursor position.
  1498. If <code>mode</code> is <code>"local"</code>, they will be
  1499. relative to the top-left corner of the editable document. If it
  1500. is <code>"page"</code> or not given, they are relative to the
  1501. top-left corner of the page. <code>where</code> can be a boolean
  1502. indicating whether you want the start (<code>true</code>) or the
  1503. end (<code>false</code>) of the selection, or, if a <code>{line,
  1504. ch}</code> object is given, it specifies the precise position at
  1505. which you want to measure.</dd>
  1506. <dt id="charCoords"><code><strong>cm.charCoords</strong>(pos: {line, ch}, ?mode: string) → {left, right, top, bottom}</code></dt>
  1507. <dd>Returns the position and dimensions of an arbitrary
  1508. character. <code>pos</code> should be a <code>{line, ch}</code>
  1509. object. This differs from <code>cursorCoords</code> in that
  1510. it'll give the size of the whole character, rather than just the
  1511. position that the cursor would have when it would sit at that
  1512. position.</dd>
  1513. <dt id="coordsChar"><code><strong>cm.coordsChar</strong>(object: {left, top}, ?mode: string) → {line, ch}</code></dt>
  1514. <dd>Given an <code>{left, top}</code> object, returns
  1515. the <code>{line, ch}</code> position that corresponds to it. The
  1516. optional <code>mode</code> parameter determines relative to what
  1517. the coordinates are interpreted. It may
  1518. be <code>"window"</code>, <code>"page"</code> (the default),
  1519. or <code>"local"</code>.</dd>
  1520. <dt id="lineAtHeight"><code><strong>cm.lineAtHeight</strong>(height: number, ?mode: string) → number</code></dt>
  1521. <dd>Computes the line at the given pixel
  1522. height. <code>mode</code> can be one of the same strings
  1523. that <a href="#coordsChar"><code>coordsChar</code></a>
  1524. accepts.</dd>
  1525. <dt id="heightAtLine"><code><strong>cm.heightAtLine</strong>(line: number, ?mode: string) → number</code></dt>
  1526. <dd>Computes the height of the top of a line, in the coordinate
  1527. system specified by <code>mode</code>
  1528. (see <a href="#coordsChar"><code>coordsChar</code></a>), which
  1529. defaults to <code>"page"</code>. When a line below the bottom of
  1530. the document is specified, the returned value is the bottom of
  1531. the last line in the document.</dd>
  1532. <dt id="defaultTextHeight"><code><strong>cm.defaultTextHeight</strong>() → number</code></dt>
  1533. <dd>Returns the line height of the default font for the editor.</dd>
  1534. <dt id="defaultCharWidth"><code><strong>cm.defaultCharWidth</strong>() → number</code></dt>
  1535. <dd>Returns the pixel width of an 'x' in the default font for
  1536. the editor. (Note that for non-monospace fonts, this is mostly
  1537. useless, and even for monospace fonts, non-ascii characters
  1538. might have a different width).</dd>
  1539. <dt id="getViewport"><code><strong>cm.getViewport</strong>() → {from: number, to: number}</code></dt>
  1540. <dd>Returns a <code>{from, to}</code> object indicating the
  1541. start (inclusive) and end (exclusive) of the currently rendered
  1542. part of the document. In big documents, when most content is
  1543. scrolled out of view, CodeMirror will only render the visible
  1544. part, and a margin around it. See also
  1545. the <a href="#event_viewportChange"><code>viewportChange</code></a>
  1546. event.</dd>
  1547. <dt id="refresh"><code><strong>cm.refresh</strong>()</code></dt>
  1548. <dd>If your code does something to change the size of the editor
  1549. element (window resizes are already listened for), or unhides
  1550. it, you should probably follow up by calling this method to
  1551. ensure CodeMirror is still looking as intended.</dd>
  1552. </dl>
  1553. <h3 id="api_mode">Mode, state, and token-related methods</h3>
  1554. <p>When writing language-aware functionality, it can often be
  1555. useful to hook into the knowledge that the CodeMirror language
  1556. mode has. See <a href="#modeapi">the section on modes</a> for a
  1557. more detailed description of how these work.</p>
  1558. <dl>
  1559. <dt id="getMode"><code><strong>doc.getMode</strong>() → object</code></dt>
  1560. <dd>Gets the (outer) mode object for the editor. Note that this
  1561. is distinct from <code>getOption("mode")</code>, which gives you
  1562. the mode specification, rather than the resolved, instantiated
  1563. <a href="#defineMode">mode object</a>.</dd>
  1564. <dt id="getModeAt"><code><strong>doc.getModeAt</strong>(pos: {line, ch}) → object</code></dt>
  1565. <dd>Gets the inner mode at a given position. This will return
  1566. the same as <a href="#getMode"><code>getMode</code></a> for
  1567. simple modes, but will return an inner mode for nesting modes
  1568. (such as <code>htmlmixed</code>).</dd>
  1569. <dt id="getTokenAt"><code><strong>cm.getTokenAt</strong>(pos: {line, ch}, ?precise: boolean) → object</code></dt>
  1570. <dd>Retrieves information about the token the current mode found
  1571. before the given position (a <code>{line, ch}</code> object). The
  1572. returned object has the following properties:
  1573. <dl>
  1574. <dt><code><strong>start</strong></code></dt><dd>The character (on the given line) at which the token starts.</dd>
  1575. <dt><code><strong>end</strong></code></dt><dd>The character at which the token ends.</dd>
  1576. <dt><code><strong>string</strong></code></dt><dd>The token's string.</dd>
  1577. <dt><code><strong>type</strong></code></dt><dd>The token type the mode assigned
  1578. to the token, such as <code>"keyword"</code>
  1579. or <code>"comment"</code> (may also be null).</dd>
  1580. <dt><code><strong>state</strong></code></dt><dd>The mode's state at the end of this token.</dd>
  1581. </dl>
  1582. If <code>precise</code> is true, the token will be guaranteed to be accurate based on recent edits. If false or
  1583. not specified, the token will use cached state information, which will be faster but might not be accurate if
  1584. edits were recently made and highlighting has not yet completed.
  1585. </dd>
  1586. <dt id="getTokenTypeAt"><code><strong>cm.getTokenTypeAt</strong>(pos: {line, ch}) → string</code></dt>
  1587. <dd>This is a (much) cheaper version
  1588. of <a href="#getTokenAt"><code>getTokenAt</code></a> useful for
  1589. when you just need the type of the token at a given position,
  1590. and no other information. Will return <code>null</code> for
  1591. unstyled tokens, and a string, potentially containing multiple
  1592. space-separated style names, otherwise.</dd>
  1593. <dt id="getHelpers"><code><strong>cm.getHelpers</strong>(pos: {line, ch}, type: string) → array&lt;helper&gt;</code></dt>
  1594. <dd>Fetch the set of applicable helper values for the given
  1595. position. Helpers provide a way to look up functionality
  1596. appropriate for a mode. The <code>type</code> argument provides
  1597. the helper namespace (see
  1598. <a href="#registerHelper"><code>registerHelper</code></a>), in
  1599. which the values will be looked up. When the mode itself has a
  1600. property that corresponds to the <code>type</code>, that
  1601. directly determines the keys that are used to look up the helper
  1602. values (it may be either a single string, or an array of
  1603. strings). Failing that, the mode's <code>helperType</code>
  1604. property and finally the mode's name are used.</dd>
  1605. <dd>For example, the JavaScript mode has a
  1606. property <code>fold</code> containing <code>"brace"</code>. When
  1607. the <code>brace-fold</code> addon is loaded, that defines a
  1608. helper named <code>brace</code> in the <code>fold</code>
  1609. namespace. This is then used by
  1610. the <a href="#addon_foldcode"><code>foldcode</code></a> addon to
  1611. figure out that it can use that folding function to fold
  1612. JavaScript code.</dd>
  1613. <dd>When any <a href="#registerGlobalHelper">'global'</a>
  1614. helpers are defined for the given namespace, their predicates
  1615. are called on the current mode and editor, and all those that
  1616. declare they are applicable will also be added to the array that
  1617. is returned.</dd>
  1618. <dt id="getHelper"><code><strong>cm.getHelper</strong>(pos: {line, ch}, type: string) → helper</code></dt>
  1619. <dd>Returns the first applicable helper value.
  1620. See <a href="#getHelpers"><code>getHelpers</code></a>.</dd>
  1621. <dt id="getStateAfter"><code><strong>cm.getStateAfter</strong>(?line: integer, ?precise: boolean) → object</code></dt>
  1622. <dd>Returns the mode's parser state, if any, at the end of the
  1623. given line number. If no line number is given, the state at the
  1624. end of the document is returned. This can be useful for storing
  1625. parsing errors in the state, or getting other kinds of
  1626. contextual information for a line. <code>precise</code> is defined
  1627. as in <code>getTokenAt()</code>.</dd>
  1628. </dl>
  1629. <h3 id="api_misc">Miscellaneous methods</h3>
  1630. <dl>
  1631. <dt id="operation"><code><strong>cm.operation</strong>(func: () → any) → any</code></dt>
  1632. <dd>CodeMirror internally buffers changes and only updates its
  1633. DOM structure after it has finished performing some operation.
  1634. If you need to perform a lot of operations on a CodeMirror
  1635. instance, you can call this method with a function argument. It
  1636. will call the function, buffering up all changes, and only doing
  1637. the expensive update after the function returns. This can be a
  1638. lot faster. The return value from this method will be the return
  1639. value of your function.</dd>
  1640. <dt id="indentLine"><code><strong>cm.indentLine</strong>(line: integer, ?dir: string|integer)</code></dt>
  1641. <dd>Adjust the indentation of the given line. The second
  1642. argument (which defaults to <code>"smart"</code>) may be one of:
  1643. <dl>
  1644. <dt><code><strong>"prev"</strong></code></dt>
  1645. <dd>Base indentation on the indentation of the previous line.</dd>
  1646. <dt><code><strong>"smart"</strong></code></dt>
  1647. <dd>Use the mode's smart indentation if available, behave
  1648. like <code>"prev"</code> otherwise.</dd>
  1649. <dt><code><strong>"add"</strong></code></dt>
  1650. <dd>Increase the indentation of the line by
  1651. one <a href="#option_indentUnit">indent unit</a>.</dd>
  1652. <dt><code><strong>"subtract"</strong></code></dt>
  1653. <dd>Reduce the indentation of the line.</dd>
  1654. <dt><code><strong>&lt;integer></strong></code></dt>
  1655. <dd>Add (positive number) or reduce (negative number) the
  1656. indentation by the given amount of spaces.</dd>
  1657. </dl></dd>
  1658. <dt id="toggleOverwrite"><code><strong>cm.toggleOverwrite</strong>(?value: bool)</code></dt>
  1659. <dd>Switches between overwrite and normal insert mode (when not
  1660. given an argument), or sets the overwrite mode to a specific
  1661. state (when given an argument).</dd>
  1662. <dt id="execCommand"><code><strong>cm.execCommand</strong>(name: string)</code></dt>
  1663. <dd>Runs the <a href="#commands">command</a> with the given name on the editor.</dd>
  1664. <dt id="posFromIndex"><code><strong>doc.posFromIndex</strong>(index: integer) → {line, ch}</code></dt>
  1665. <dd>Calculates and returns a <code>{line, ch}</code> object for a
  1666. zero-based <code>index</code> who's value is relative to the start of the
  1667. editor's text. If the <code>index</code> is out of range of the text then
  1668. the returned object is clipped to start or end of the text
  1669. respectively.</dd>
  1670. <dt id="indexFromPos"><code><strong>doc.indexFromPos</strong>(object: {line, ch}) → integer</code></dt>
  1671. <dd>The reverse of <a href="#posFromIndex"><code>posFromIndex</code></a>.</dd>
  1672. <dt id="focus"><code><strong>cm.focus</strong>()</code></dt>
  1673. <dd>Give the editor focus.</dd>
  1674. <dt id="getInputField"><code><strong>cm.getInputField</strong>() → TextAreaElement</code></dt>
  1675. <dd>Returns the hidden textarea used to read input.</dd>
  1676. <dt id="getWrapperElement"><code><strong>cm.getWrapperElement</strong>() → Element</code></dt>
  1677. <dd>Returns the DOM node that represents the editor, and
  1678. controls its size. Remove this from your tree to delete an
  1679. editor instance.</dd>
  1680. <dt id="getScrollerElement"><code><strong>cm.getScrollerElement</strong>() → Element</code></dt>
  1681. <dd>Returns the DOM node that is responsible for the scrolling
  1682. of the editor.</dd>
  1683. <dt id="getGutterElement"><code><strong>cm.getGutterElement</strong>() → Element</code></dt>
  1684. <dd>Fetches the DOM node that contains the editor gutters.</dd>
  1685. </dl>
  1686. <h3 id="api_static">Static properties</h3>
  1687. <p>The <code>CodeMirror</code> object itself provides
  1688. several useful properties.</p>
  1689. <dl>
  1690. <dt id="version"><code><strong>CodeMirror.version</strong>: string</code></dt>
  1691. <dd>It contains a string that indicates the version of the
  1692. library. This is a triple of
  1693. integers <code>"major.minor.patch"</code>,
  1694. where <code>patch</code> is zero for releases, and something
  1695. else (usually one) for dev snapshots.</dd>
  1696. <dt id="fromTextArea"><code><strong>CodeMirror.fromTextArea</strong>(textArea: TextAreaElement, ?config: object)</code></dt>
  1697. <dd>
  1698. The method provides another way to initialize an editor. It
  1699. takes a textarea DOM node as first argument and an optional
  1700. configuration object as second. It will replace the textarea
  1701. with a CodeMirror instance, and wire up the form of that
  1702. textarea (if any) to make sure the editor contents are put
  1703. into the textarea when the form is submitted. The text in the
  1704. textarea will provide the content for the editor. A CodeMirror
  1705. instance created this way has three additional methods:
  1706. <dl>
  1707. <dt id="save"><code><strong>cm.save</strong>()</code></dt>
  1708. <dd>Copy the content of the editor into the textarea.</dd>
  1709. <dt id="toTextArea"><code><strong>cm.toTextArea</strong>()</code></dt>
  1710. <dd>Remove the editor, and restore the original textarea (with
  1711. the editor's current content).</dd>
  1712. <dt id="getTextArea"><code><strong>cm.getTextArea</strong>() → TextAreaElement</code></dt>
  1713. <dd>Returns the textarea that the instance was based on.</dd>
  1714. </dl>
  1715. </dd>
  1716. <dt id="defaults"><code><strong>CodeMirror.defaults</strong>: object</code></dt>
  1717. <dd>An object containing default values for
  1718. all <a href="#config">options</a>. You can assign to its
  1719. properties to modify defaults (though this won't affect editors
  1720. that have already been created).</dd>
  1721. <dt id="defineExtension"><code><strong>CodeMirror.defineExtension</strong>(name: string, value: any)</code></dt>
  1722. <dd>If you want to define extra methods in terms of the
  1723. CodeMirror API, it is possible to
  1724. use <code>defineExtension</code>. This will cause the given
  1725. value (usually a method) to be added to all CodeMirror instances
  1726. created from then on.</dd>
  1727. <dt id="defineDocExtension"><code><strong>CodeMirror.defineDocExtension</strong>(name: string, value: any)</code></dt>
  1728. <dd>Like <a href="#defineExtenstion"><code>defineExtension</code></a>,
  1729. but the method will be added to the interface
  1730. for <a href="#Doc"><code>Doc</code></a> objects instead.</dd>
  1731. <dt id="defineOption"><code><strong>CodeMirror.defineOption</strong>(name: string,
  1732. default: any, updateFunc: function)</code></dt>
  1733. <dd>Similarly, <code>defineOption</code> can be used to define new options for
  1734. CodeMirror. The <code>updateFunc</code> will be called with the
  1735. editor instance and the new value when an editor is initialized,
  1736. and whenever the option is modified
  1737. through <a href="#setOption"><code>setOption</code></a>.</dd>
  1738. <dt id="defineInitHook"><code><strong>CodeMirror.defineInitHook</strong>(func: function)</code></dt>
  1739. <dd>If your extention just needs to run some
  1740. code whenever a CodeMirror instance is initialized,
  1741. use <code>CodeMirror.defineInitHook</code>. Give it a function as
  1742. its only argument, and from then on, that function will be called
  1743. (with the instance as argument) whenever a new CodeMirror instance
  1744. is initialized.</dd>
  1745. <dt id="registerHelper"><code><strong>CodeMirror.registerHelper</strong>(type: string, name: string, value: helper)</code></dt>
  1746. <dd>Registers a helper value with the given <code>name</code> in
  1747. the given namespace (<code>type</code>). This is used to define
  1748. functionality that may be looked up by mode. Will create (if it
  1749. doesn't already exist) a property on the <code>CodeMirror</code>
  1750. object for the given <code>type</code>, pointing to an object
  1751. that maps names to values. I.e. after
  1752. doing <code>CodeMirror.registerHelper("hint", "foo",
  1753. myFoo)</code>, the value <code>CodeMirror.hint.foo</code> will
  1754. point to <code>myFoo</code>.</dd>
  1755. <dt id="registerGlobalHelper"><code><strong>CodeMirror.registerGlobalHelper</strong>(type: string, name: string, predicate: fn(mode, CodeMirror), value: helper)</code></dt>
  1756. <dd>Acts
  1757. like <a href="#registerHelper"><code>registerHelper</code></a>,
  1758. but also registers this helper as 'global', meaning that it will
  1759. be included by <a href="#getHelpers"><code>getHelpers</code></a>
  1760. whenever the given <code>predicate</code> returns true when
  1761. called with the local mode and editor.</dd>
  1762. <dt id="Pos"><code><strong>CodeMirror.Pos</strong>(line: integer, ?ch: integer)</code></dt>
  1763. <dd>A constructor for the <code>{line, ch}</code> objects that
  1764. are used to represent positions in editor documents.</dd>
  1765. <dt id="changeEnd"><code><strong>CodeMirror.changeEnd</strong>(change: object) → {line, ch}</code></dt>
  1766. <dd>Utility function that computes an end position from a change
  1767. (an object with <code>from</code>, <code>to</code>,
  1768. and <code>text</code> properties, as passed to
  1769. various <a href="#event_change">event handlers</a>). The
  1770. returned position will be the end of the changed
  1771. range, <em>after</em> the change is applied.</dd>
  1772. </dl>
  1773. </section>
  1774. <section id=addons>
  1775. <h2>Addons</h2>
  1776. <p>The <code>addon</code> directory in the distribution contains a
  1777. number of reusable components that implement extra editor
  1778. functionality (on top of extension functions
  1779. like <a href="#defineOption"><code>defineOption</code></a>, <a href="#defineExtension"><code>defineExtension</code></a>,
  1780. and <a href="#registerHelper"><code>registerHelper</code></a>). In
  1781. brief, they are:</p>
  1782. <dl>
  1783. <dt id="addon_dialog"><a href="../addon/dialog/dialog.js"><code>dialog/dialog.js</code></a></dt>
  1784. <dd>Provides a very simple way to query users for text input.
  1785. Adds the <strong><code>openDialog(template, callback, options) →
  1786. closeFunction</code></strong> method to CodeMirror instances,
  1787. which can be called with an HTML fragment or a detached DOM
  1788. node that provides the prompt (should include an <code>input</code>
  1789. or <code>button</code> tag), and a callback function that is called
  1790. when the user presses enter. It returns a function <code>closeFunction</code>
  1791. which, if called, will close the dialog immediately.
  1792. <strong><code>openDialog</code></strong> takes the following options:
  1793. <dl>
  1794. <dt><code><strong>closeOnEnter</strong></code>:</dt>
  1795. <dd>If true, the dialog will be closed when the user presses
  1796. enter in the input. Defaults to <code>true</code>.</dd>
  1797. <dt><code><strong>onKeyDown</strong></code>:</dt>
  1798. <dd>An event handler of the signature <code>(event, value, closeFunction)</code>
  1799. that will be called whenever <code>keydown</code> fires in the
  1800. dialog's input. If your callback returns <code>true</code>,
  1801. the dialog will not do any further processing of the event.</dd>
  1802. <dt><code><strong>onKeyUp</strong></code>:</dt>
  1803. <dd>Same as <code>onKeyDown</code> but for the
  1804. <code>keyup</code> event.</dd>
  1805. <dt><code><strong>onInput</strong></code>:</dt>
  1806. <dd>Same as <code>onKeyDown</code> but for the
  1807. <code>input</code> event.</dd>
  1808. <dt><code><strong>onClose</strong></code>:</dt>
  1809. <dd>A callback of the signature <code>(dialogInstance)</code>
  1810. that will be called after the dialog has been closed and
  1811. removed from the DOM. No return value.</dd>
  1812. </dl>
  1813. <p>Also adds an <strong><code>openNotification(template, options) →
  1814. closeFunction</code></strong> function that simply shows an HTML
  1815. fragment as a notification at the top of the editor. It takes a
  1816. single option: <code>duration</code>, the amount of time after
  1817. which the notification will be automatically closed. If <code>
  1818. duration</code> is zero, the dialog will not be closed automatically.</p>
  1819. <p>Depends on <code>addon/dialog/dialog.css</code>.</p></dd>
  1820. <dt id="addon_searchcursor"><a href="../addon/search/searchcursor.js"><code>search/searchcursor.js</code></a></dt>
  1821. <dd>Adds the <code>getSearchCursor(query, start, caseFold) →
  1822. cursor</code> method to CodeMirror instances, which can be used
  1823. to implement search/replace functionality. <code>query</code>
  1824. can be a regular expression or a string (only strings will match
  1825. across lines—if they contain newlines). <code>start</code>
  1826. provides the starting position of the search. It can be
  1827. a <code>{line, ch}</code> object, or can be left off to default
  1828. to the start of the document. <code>caseFold</code> is only
  1829. relevant when matching a string. It will cause the search to be
  1830. case-insensitive. A search cursor has the following methods:
  1831. <dl>
  1832. <dt><code><strong>findNext</strong>() → boolean</code></dt>
  1833. <dt><code><strong>findPrevious</strong>() → boolean</code></dt>
  1834. <dd>Search forward or backward from the current position.
  1835. The return value indicates whether a match was found. If
  1836. matching a regular expression, the return value will be the
  1837. array returned by the <code>match</code> method, in case you
  1838. want to extract matched groups.</dd>
  1839. <dt><code><strong>from</strong>() → {line, ch}</code></dt>
  1840. <dt><code><strong>to</strong>() → {line, ch}</code></dt>
  1841. <dd>These are only valid when the last call
  1842. to <code>findNext</code> or <code>findPrevious</code> did
  1843. not return false. They will return <code>{line, ch}</code>
  1844. objects pointing at the start and end of the match.</dd>
  1845. <dt><code><strong>replace</strong>(text: string)</code></dt>
  1846. <dd>Replaces the currently found match with the given text
  1847. and adjusts the cursor position to reflect the
  1848. replacement.</dd>
  1849. </dl></dd>
  1850. <dt id="addon_search"><a href="../addon/search/search.js"><code>search/search.js</code></a></dt>
  1851. <dd>Implements the search commands. CodeMirror has keys bound to
  1852. these by default, but will not do anything with them unless an
  1853. implementation is provided. Depends
  1854. on <code>searchcursor.js</code>, and will make use
  1855. of <a href="#addon_dialog"><code>openDialog</code></a> when
  1856. available to make prompting for search queries less ugly.</dd>
  1857. <dt id="addon_matchbrackets"><a href="../addon/edit/matchbrackets.js"><code>edit/matchbrackets.js</code></a></dt>
  1858. <dd>Defines an option <code>matchBrackets</code> which, when set
  1859. to true, causes matching brackets to be highlighted whenever the
  1860. cursor is next to them. It also adds a
  1861. method <code>matchBrackets</code> that forces this to happen
  1862. once, and a method <code>findMatchingBracket</code> that can be
  1863. used to run the bracket-finding algorithm that this uses
  1864. internally.</dd>
  1865. <dt id="addon_closebrackets"><a href="../addon/edit/closebrackets.js"><code>edit/closebrackets.js</code></a></dt>
  1866. <dd>Defines an option <code>autoCloseBrackets</code> that will
  1867. auto-close brackets and quotes when typed. By default, it'll
  1868. auto-close <code>()[]{}''""</code>, but you can pass it a string
  1869. similar to that (containing pairs of matching characters), or an
  1870. object with <code>pairs</code> and
  1871. optionally <code>explode</code> properties to customize
  1872. it. <code>explode</code> should be a similar string that gives
  1873. the pairs of characters that, when enter is pressed between
  1874. them, should have the second character also moved to its own
  1875. line. <a href="../demo/closebrackets.html">Demo here</a>.</dd>
  1876. <dt id="addon_matchtags"><a href="../addon/edit/matchtags.js"><code>edit/matchtags.js</code></a></dt>
  1877. <dd>Defines an option <code>matchTags</code> that, when enabled,
  1878. will cause the tags around the cursor to be highlighted (using
  1879. the <code>CodeMirror-matchingtag</code> class). Also
  1880. defines
  1881. a <a href="#commands">command</a> <code>toMatchingTag</code>,
  1882. which you can bind a key to in order to jump to the tag mathing
  1883. the one under the cursor. Depends on
  1884. the <code>addon/fold/xml-fold.js</code>
  1885. addon. <a href="../demo/matchtags.html">Demo here.</a></dd>
  1886. <dt id="addon_trailingspace"><a href="../addon/edit/trailingspace.js"><code>edit/trailingspace.js</code></a></dt>
  1887. <dd>Adds an option <code>showTrailingSpace</code> which, when
  1888. enabled, adds the CSS class <code>cm-trailingspace</code> to
  1889. stretches of whitespace at the end of lines.
  1890. The <a href="../demo/trailingspace.html">demo</a> has a nice
  1891. squiggly underline style for this class.</dd>
  1892. <dt id="addon_closetag"><a href="../addon/edit/closetag.js"><code>edit/closetag.js</code></a></dt>
  1893. <dd>Provides utility functions for adding automatic tag closing
  1894. to XML modes. See
  1895. the <a href="../demo/closetag.html">demo</a>.</dd>
  1896. <dt id="addon_continuelist"><a href="../addon/edit/continuelist.js"><code>edit/continuelist.js</code></a></dt>
  1897. <dd>Markdown specific. Defines
  1898. a <code>"newlineAndIndentContinueMarkdownList"</code> <a href="#commands">command</a>
  1899. command that can be bound to <code>enter</code> to automatically
  1900. insert the leading characters for continuing a list. See
  1901. the <a href="../mode/markdown/index.html">Markdown mode
  1902. demo</a>.</dd>
  1903. <dt id="addon_comment"><a href="../addon/comment/comment.js"><code>comment/comment.js</code></a></dt>
  1904. <dd>Addon for commenting and uncommenting code. Adds three
  1905. methods to CodeMirror instances:
  1906. <dl>
  1907. <dt id="lineComment"><code><strong>lineComment</strong>(from: {line, ch}, to: {line, ch}, ?options: object)</code></dt>
  1908. <dd>Set the lines in the given range to be line comments. Will
  1909. fall back to <code>blockComment</code> when no line comment
  1910. style is defined for the mode.</dd>
  1911. <dt id="blockComment"><code><strong>blockComment</strong>(from: {line, ch}, to: {line, ch}, ?options: object)</code></dt>
  1912. <dd>Wrap the code in the given range in a block comment. Will
  1913. fall back to <code>lineComment</code> when no block comment
  1914. style is defined for the mode.</dd>
  1915. <dt id="uncomment"><code><strong>uncomment</strong>(from: {line, ch}, to: {line, ch}, ?options: object) → boolean</code></dt>
  1916. <dd>Try to uncomment the given range.
  1917. Returns <code>true</code> if a comment range was found and
  1918. removed, <code>false</code> otherwise.</dd>
  1919. </dl>
  1920. The <code>options</code> object accepted by these methods may
  1921. have the following properties:
  1922. <dl>
  1923. <dt><code>blockCommentStart, blockCommentEnd, blockCommentLead, lineComment: string</code></dt>
  1924. <dd>Override the <a href="#mode_comment">comment string
  1925. properties</a> of the mode with custom comment strings.</dd>
  1926. <dt><code><strong>padding</strong>: string</code></dt>
  1927. <dd>A string that will be inserted after opening and leading
  1928. markers, and before closing comment markers. Defaults to a
  1929. single space.</dd>
  1930. <dt><code><strong>commentBlankLines</strong>: boolean</code></dt>
  1931. <dd>Whether, when adding line comments, to also comment lines
  1932. that contain only whitespace.</dd>
  1933. <dt><code><strong>indent</strong>: boolean</code></dt>
  1934. <dd>When adding line comments and this is turned on, it will
  1935. align the comment block to the current indentation of the
  1936. first line of the block.</dd>
  1937. <dt><code><strong>fullLines</strong>: boolean</code></dt>
  1938. <dd>When block commenting, this controls whether the whole
  1939. lines are indented, or only the precise range that is given.
  1940. Defaults to <code>true</code>.</dd>
  1941. </dl>
  1942. The addon also defines
  1943. a <code>toggleComment</code> <a href="#commands">command</a>,
  1944. which will try to uncomment the current selection, and if that
  1945. fails, line-comments it.</dd>
  1946. <dt id="addon_foldcode"><a href="../addon/fold/foldcode.js"><code>fold/foldcode.js</code></a></dt>
  1947. <dd>Helps with code folding. Adds a <code>foldCode</code> method
  1948. to editor instances, which will try to do a code fold starting
  1949. at the given line, or unfold the fold that is already present.
  1950. The method takes as first argument the position that should be
  1951. folded (may be a line number or
  1952. a <a href="#Pos"><code>Pos</code></a>), and as second optional
  1953. argument either a range-finder function, or an options object,
  1954. supporting the following properties:
  1955. <dl>
  1956. <dt><code><strong>rangeFinder</strong>: fn(CodeMirror, Pos)</code></dt>
  1957. <dd id="helper_fold_auto">The function that is used to find
  1958. foldable ranges. If this is not directly passed, it will
  1959. default to <code>CodeMirror.fold.auto</code>, which
  1960. uses <a href="#getHelpers"><code>getHelpers</code></a> with
  1961. a <code>"fold"</code> type to find folding functions
  1962. appropriate for the local mode. There are files in
  1963. the <a href="../addon/fold/"><code>addon/fold/</code></a>
  1964. directory providing <code>CodeMirror.fold.brace</code>, which
  1965. finds blocks in brace languages (JavaScript, C, Java,
  1966. etc), <code>CodeMirror.fold.indent</code>, for languages where
  1967. indentation determines block structure (Python, Haskell),
  1968. and <code>CodeMirror.fold.xml</code>, for XML-style languages,
  1969. and <code>CodeMirror.fold.comment</code>, for folding comment
  1970. blocks.</dd>
  1971. <dt><code><strong>widget</strong>: string|Element</code></dt>
  1972. <dd>The widget to show for folded ranges. Can be either a
  1973. string, in which case it'll become a span with
  1974. class <code>CodeMirror-foldmarker</code>, or a DOM node.</dd>
  1975. <dt><code><strong>scanUp</strong>: boolean</code></dt>
  1976. <dd>When true (default is false), the addon will try to find
  1977. foldable ranges on the lines above the current one if there
  1978. isn't an eligible one on the given line.</dd>
  1979. <dt><code><strong>minFoldSize</strong>: integer</code></dt>
  1980. <dd>The minimum amount of lines that a fold should span to be
  1981. accepted. Defaults to 0, which also allows single-line
  1982. folds.</dd>
  1983. </dl>
  1984. See <a href="../demo/folding.html">the demo</a> for an
  1985. example.</dd>
  1986. <dt id="addon_foldgutter"><a href="../addon/fold/foldgutter.js"><code>fold/foldgutter.js</code></a></dt>
  1987. <dd>Provides an option <code>foldGutter</code>, which can be
  1988. used to create a gutter with markers indicating the blocks that
  1989. can be folded. Create a gutter using
  1990. the <a href="#option_gutters"><code>gutters</code></a> option,
  1991. giving it the class <code>CodeMirror-foldgutter</code> or
  1992. something else if you configure the addon to use a different
  1993. class, and this addon will show markers next to folded and
  1994. foldable blocks, and handle clicks in this gutter. Note that
  1995. CSS styles should be applied to make the gutter, and the fold
  1996. markers within it, visible. A default set of CSS styles are
  1997. available in:
  1998. <a href="../addon/fold/foldgutter.css">
  1999. <code>addon/fold/foldgutter.css</code>
  2000. </a>.
  2001. The option
  2002. can be either set to <code>true</code>, or an object containing
  2003. the following optional option fields:
  2004. <dl>
  2005. <dt><code><strong>gutter</strong>: string</code></dt>
  2006. <dd>The CSS class of the gutter. Defaults
  2007. to <code>"CodeMirror-foldgutter"</code>. You will have to
  2008. style this yourself to give it a width (and possibly a
  2009. background). See the default gutter style rules above.</dd>
  2010. <dt><code><strong>indicatorOpen</strong>: string | Element</code></dt>
  2011. <dd>A CSS class or DOM element to be used as the marker for
  2012. open, foldable blocks. Defaults
  2013. to <code>"CodeMirror-foldgutter-open"</code>.</dd>
  2014. <dt><code><strong>indicatorFolded</strong>: string | Element</code></dt>
  2015. <dd>A CSS class or DOM element to be used as the marker for
  2016. folded blocks. Defaults to <code>"CodeMirror-foldgutter-folded"</code>.</dd>
  2017. <dt><code><strong>rangeFinder</strong>: fn(CodeMirror, Pos)</code></dt>
  2018. <dd>The range-finder function to use when determining whether
  2019. something can be folded. When not
  2020. given, <a href="#helper_fold_auto"><code>CodeMirror.fold.auto</code></a>
  2021. will be used as default.</dd>
  2022. </dl>
  2023. The <code>foldOptions</code> editor option can be set to an
  2024. object to provide an editor-wide default configuration.
  2025. Demo <a href="../demo/folding.html">here</a>.</dd>
  2026. <dt id="addon_runmode"><a href="../addon/runmode/runmode.js"><code>runmode/runmode.js</code></a></dt>
  2027. <dd>Can be used to run a CodeMirror mode over text without
  2028. actually opening an editor instance.
  2029. See <a href="../demo/runmode.html">the demo</a> for an example.
  2030. There are alternate versions of the file avaible for
  2031. running <a href="../addon/runmode/runmode-standalone.js">stand-alone</a>
  2032. (without including all of CodeMirror) and
  2033. for <a href="../addon/runmode/runmode.node.js">running under
  2034. node.js</a>.</dd>
  2035. <dt id="addon_colorize"><a href="../addon/runmode/colorize.js"><code>runmode/colorize.js</code></a></dt>
  2036. <dd>Provides a convenient way to syntax-highlight code snippets
  2037. in a webpage. Depends on
  2038. the <a href="#addon_runmode"><code>runmode</code></a> addon (or
  2039. its standalone variant). Provides
  2040. a <code>CodeMirror.colorize</code> function that can be called
  2041. with an array (or other array-ish collection) of DOM nodes that
  2042. represent the code snippets. By default, it'll get
  2043. all <code>pre</code> tags. Will read the <code>data-lang</code>
  2044. attribute of these nodes to figure out their language, and
  2045. syntax-color their content using the relevant CodeMirror mode
  2046. (you'll have to load the scripts for the relevant modes
  2047. yourself). A second argument may be provided to give a default
  2048. mode, used when no language attribute is found for a node. Used
  2049. in this manual to highlight example code.</dd>
  2050. <dt id="addon_overlay"><a href="../addon/mode/overlay.js"><code>mode/overlay.js</code></a></dt>
  2051. <dd>Mode combinator that can be used to extend a mode with an
  2052. 'overlay' — a secondary mode is run over the stream, along with
  2053. the base mode, and can color specific pieces of text without
  2054. interfering with the base mode.
  2055. Defines <code>CodeMirror.overlayMode</code>, which is used to
  2056. create such a mode. See <a href="../demo/mustache.html">this
  2057. demo</a> for a detailed example.</dd>
  2058. <dt id="addon_multiplex"><a href="../addon/mode/multiplex.js"><code>mode/multiplex.js</code></a></dt>
  2059. <dd>Mode combinator that can be used to easily 'multiplex'
  2060. between several modes.
  2061. Defines <code>CodeMirror.multiplexingMode</code> which, when
  2062. given as first argument a mode object, and as other arguments
  2063. any number of <code>{open, close, mode [, delimStyle, innerStyle]}</code>
  2064. objects, will return a mode object that starts parsing using the
  2065. mode passed as first argument, but will switch to another mode
  2066. as soon as it encounters a string that occurs in one of
  2067. the <code>open</code> fields of the passed objects. When in a
  2068. sub-mode, it will go back to the top mode again when
  2069. the <code>close</code> string is encountered.
  2070. Pass <code>"\n"</code> for <code>open</code> or <code>close</code>
  2071. if you want to switch on a blank line.
  2072. <ul><li>When <code>delimStyle</code> is specified, it will be the token
  2073. style returned for the delimiter tokens.</li>
  2074. <li>When <code>innerStyle</code> is specified, it will be the token
  2075. style added for each inner mode token.</li></ul>
  2076. The outer mode will not see the content between the delimiters.
  2077. See <a href="../demo/multiplex.html">this demo</a> for an
  2078. example.</dd>
  2079. <dt id="addon_show-hint"><a href="../addon/hint/show-hint.js"><code>hint/show-hint.js</code></a></dt>
  2080. <dd>Provides a framework for showing autocompletion hints.
  2081. Defines <code>editor.showHint</code>, which takes an optional
  2082. options object, and pops up a widget that allows the user to
  2083. select a completion. Finding hints is done with a hinting
  2084. functions (the <code>hint</code> option), which is a function
  2085. that take an editor instance and options object, and return
  2086. a <code>{list, from, to}</code> object, where <code>list</code>
  2087. is an array of strings or objects (the completions),
  2088. and <code>from</code> and <code>to</code> give the start and end
  2089. of the token that is being completed as <code>{line, ch}</code>
  2090. objects.</dd>
  2091. <dd>If no hinting function is given, the addon will
  2092. use <code>CodeMirror.hint.auto</code>, which
  2093. calls <a href="#getHelpers"><code>getHelpers</code></a> with
  2094. the <code>"hint"</code> type to find applicable hinting
  2095. functions, and tries them one by one. If that fails, it looks
  2096. for a <code>"hintWords"</code> helper to fetch a list of
  2097. completable words for the mode, and
  2098. uses <code>CodeMirror.hint.fromList</code> to complete from
  2099. those.</dd>
  2100. <dd>When completions aren't simple strings, they should be
  2101. objects with the following properties:
  2102. <dl>
  2103. <dt><code><strong>text</strong>: string</code></dt>
  2104. <dd>The completion text. This is the only required
  2105. property.</dd>
  2106. <dt><code><strong>displayText</strong>: string</code></dt>
  2107. <dd>The text that should be displayed in the menu.</dd>
  2108. <dt><code><strong>className</strong>: string</code></dt>
  2109. <dd>A CSS class name to apply to the completion's line in the
  2110. menu.</dd>
  2111. <dt><code><strong>render</strong>: fn(Element, self, data)</code></dt>
  2112. <dd>A method used to create the DOM structure for showing the
  2113. completion by appending it to its first argument.</dd>
  2114. <dt><code><strong>hint</strong>: fn(CodeMirror, self, data)</code></dt>
  2115. <dd>A method used to actually apply the completion, instead of
  2116. the default behavior.</dd>
  2117. <dt><code><strong>from</strong>: {line, ch}</code></dt>
  2118. <dd>Optional <code>from</code> position that will be used by <code>pick()</code> instead
  2119. of the global one passed with the full list of completions.</dd>
  2120. <dt><code><strong>to</strong>: {line, ch}</code></dt>
  2121. <dd>Optional <code>to</code> position that will be used by <code>pick()</code> instead
  2122. of the global one passed with the full list of completions.</dd>
  2123. </dl>
  2124. The plugin understands the following options (the options object
  2125. will also be passed along to the hinting function, which may
  2126. understand additional options):
  2127. <dl>
  2128. <dt><code><strong>hint</strong>: function</code></dt>
  2129. <dd>A hinting function, as specified above. It is possible to
  2130. set the <code>async</code> property on a hinting function to
  2131. true, in which case it will be called with
  2132. arguments <code>(cm, callback, ?options)</code>, and the
  2133. completion interface will only be popped up when the hinting
  2134. function calls the callback, passing it the object holding the
  2135. completions.</dd>
  2136. <dt><code><strong>completeSingle</strong>: boolean</code></dt>
  2137. <dd>Determines whether, when only a single completion is
  2138. available, it is completed without showing the dialog.
  2139. Defaults to true.</dd>
  2140. <dt><code><strong>alignWithWord</strong>: boolean</code></dt>
  2141. <dd>Whether the pop-up should be horizontally aligned with the
  2142. start of the word (true, default), or with the cursor (false).</dd>
  2143. <dt><code><strong>closeOnUnfocus</strong>: boolean</code></dt>
  2144. <dd>When enabled (which is the default), the pop-up will close
  2145. when the editor is unfocused.</dd>
  2146. <dt><code><strong>customKeys</strong>: keymap</code></dt>
  2147. <dd>Allows you to provide a custom key map of keys to be active
  2148. when the pop-up is active. The handlers will be called with an
  2149. extra argument, a handle to the completion menu, which
  2150. has <code>moveFocus(n)</code>, <code>setFocus(n)</code>, <code>pick()</code>,
  2151. and <code>close()</code> methods (see the source for details),
  2152. that can be used to change the focused element, pick the
  2153. current element or close the menu. Additionnaly <code>menuSize()</code>
  2154. can give you access to the size of the current dropdown menu,
  2155. <code>length</code> give you the number of availlable completions, and
  2156. <code>data</code> give you full access to the completion returned by the
  2157. hinting function.</dd>
  2158. <dt><code><strong>extraKeys</strong>: keymap</code></dt>
  2159. <dd>Like <code>customKeys</code> above, but the bindings will
  2160. be added to the set of default bindings, instead of replacing
  2161. them.</dd>
  2162. </dl>
  2163. The following events will be fired on the completions object
  2164. during completion:
  2165. <dl>
  2166. <dt><code><strong>"shown"</strong> ()</code></dt>
  2167. <dd>Fired when the pop-up is shown.</dd>
  2168. <dt><code><strong>"select"</strong> (completion, Element)</code></dt>
  2169. <dd>Fired when a completion is selected. Passed the completion
  2170. value (string or object) and the DOM node that represents it
  2171. in the menu.</dd>
  2172. <dt><code><strong>"pick"</strong> (completion)</code></dt>
  2173. <dd>Fired when a completion is picked. Passed the completion value
  2174. (string or object).</dd>
  2175. <dt><code><strong>"close"</strong> ()</code></dt>
  2176. <dd>Fired when the completion is finished.</dd>
  2177. </dl>
  2178. This addon depends on styles
  2179. from <code>addon/hint/show-hint.css</code>. Check
  2180. out <a href="../demo/complete.html">the demo</a> for an
  2181. example.</dd>
  2182. <dt id="addon_javascript-hint"><a href="../addon/hint/javascript-hint.js"><code>hint/javascript-hint.js</code></a></dt>
  2183. <dd>Defines a simple hinting function for JavaScript
  2184. (<code>CodeMirror.hint.javascript</code>) and CoffeeScript
  2185. (<code>CodeMirror.hint.coffeescript</code>) code. This will
  2186. simply use the JavaScript environment that the editor runs in as
  2187. a source of information about objects and their properties.</dd>
  2188. <dt id="addon_xml-hint"><a href="../addon/hint/xml-hint.js"><code>hint/xml-hint.js</code></a></dt>
  2189. <dd>Defines <code>CodeMirror.hint.xml</code>, which produces
  2190. hints for XML tagnames, attribute names, and attribute values,
  2191. guided by a <code>schemaInfo</code> option (a property of the
  2192. second argument passed to the hinting function, or the third
  2193. argument passed to <code>CodeMirror.showHint</code>).<br>The
  2194. schema info should be an object mapping tag names to information
  2195. about these tags, with optionally a <code>"!top"</code> property
  2196. containing a list of the names of valid top-level tags. The
  2197. values of the properties should be objects with optional
  2198. properties <code>children</code> (an array of valid child
  2199. element names, omit to simply allow all tags to appear)
  2200. and <code>attrs</code> (an object mapping attribute names
  2201. to <code>null</code> for free-form attributes, and an array of
  2202. valid values for restricted
  2203. attributes). <a href="../demo/xmlcomplete.html">Demo
  2204. here.</a></dd>
  2205. <dt id="addon_html-hint"><a href="../addon/hint/html-hint.js"><code>hint/html-hint.js</code></a></dt>
  2206. <dd>Provides schema info to
  2207. the <a href="#addon_xml-hint">xml-hint</a> addon for HTML
  2208. documents. Defines a schema
  2209. object <code>CodeMirror.htmlSchema</code> that you can pass to
  2210. as a <code>schemaInfo</code> option, and
  2211. a <code>CodeMirror.hint.html</code> hinting function that
  2212. automatically calls <code>CodeMirror.hint.xml</code> with this
  2213. schema data. See
  2214. the <a href="../demo/html5complete.html">demo</a>.</dd>
  2215. <dt id="addon_css-hint"><a href="../addon/hint/css-hint.js"><code>hint/css-hint.js</code></a></dt>
  2216. <dd>A hinting function for CSS, SCSS, or LESS code.
  2217. Defines <code>CodeMirror.hint.css</code>.</dd>
  2218. <dt id="addon_python-hint"><a href="../addon/hint/python-hint.js"><code>hint/python-hint.js</code></a></dt>
  2219. <dd>A very simple hinting function for Python code.
  2220. Defines <code>CodeMirror.hint.python</code>.</dd>
  2221. <dt id="addon_anyword-hint"><a href="../addon/hint/anyword-hint.js"><code>hint/anyword-hint.js</code></a></dt>
  2222. <dd>A very simple hinting function
  2223. (<code>CodeMirror.hint.anyword</code>) that simply looks for
  2224. words in the nearby code and completes to those. Takes two
  2225. optional options, <code>word</code>, a regular expression that
  2226. matches words (sequences of one or more character),
  2227. and <code>range</code>, which defines how many lines the addon
  2228. should scan when completing (defaults to 500).</dd>
  2229. <dt id="addon_sql-hint"><a href="../addon/hint/sql-hint.js"><code>hint/sql-hint.js</code></a></dt>
  2230. <dd>A simple SQL hinter. Defines <code>CodeMirror.hint.sql</code>.</dd>
  2231. <dt id="addon_match-highlighter"><a href="../addon/search/match-highlighter.js"><code>search/match-highlighter.js</code></a></dt>
  2232. <dd>Adds a <code>highlightSelectionMatches</code> option that
  2233. can be enabled to highlight all instances of a currently
  2234. selected word. Can be set either to true or to an object
  2235. containing the following options: <code>minChars</code>, for the
  2236. minimum amount of selected characters that triggers a highlight
  2237. (default 2), <code>style</code>, for the style to be used to
  2238. highlight the matches (default <code>"matchhighlight"</code>,
  2239. which will correspond to CSS
  2240. class <code>cm-matchhighlight</code>),
  2241. and <code>showToken</code> which can be set to <code>true</code>
  2242. or to a regexp matching the characters that make up a word. When
  2243. enabled, it causes the current word to be highlighted when
  2244. nothing is selected (defaults to off).
  2245. Demo <a href="../demo/matchhighlighter.html">here</a>.</dd>
  2246. <dt id="addon_lint"><a href="../addon/lint/lint.js"><code>lint/lint.js</code></a></dt>
  2247. <dd>Defines an interface component for showing linting warnings,
  2248. with pluggable warning sources
  2249. (see <a href="../addon/lint/json-lint.js"><code>json-lint.js</code></a>,
  2250. <a href="../addon/lint/javascript-lint.js"><code>javascript-lint.js</code></a>,
  2251. and <a href="../addon/lint/css-lint.js"><code>css-lint.js</code></a>
  2252. in the same directory). Defines a <code>lint</code> option that
  2253. can be set to a warning source (for
  2254. example <code>CodeMirror.lint.javascript</code>), or
  2255. to <code>true</code>, in which
  2256. case <a href="#getHelper"><code>getHelper</code></a> with
  2257. type <code>"lint"</code> is used to determined a validator
  2258. function. Depends on <code>addon/lint/lint.css</code>. A demo
  2259. can be found <a href="../demo/lint.html">here</a>.</dd>
  2260. <dt id="addon_mark-selection"><a href="../addon/selection/mark-selection.js"><code>selection/mark-selection.js</code></a></dt>
  2261. <dd>Causes the selected text to be marked with the CSS class
  2262. <code>CodeMirror-selectedtext</code> when the <code>styleSelectedText</code> option
  2263. is enabled. Useful to change the colour of the selection (in addition to the background),
  2264. like in <a href="../demo/markselection.html">this demo</a>.</dd>
  2265. <dt id="addon_active-line"><a href="../addon/selection/active-line.js"><code>selection/active-line.js</code></a></dt>
  2266. <dd>Defines a <code>styleActiveLine</code> option that, when enabled,
  2267. gives the wrapper of the active line the class <code>CodeMirror-activeline</code>,
  2268. and adds a background with the class <code>CodeMirror-activeline-background</code>.
  2269. is enabled. See the <a href="../demo/activeline.html">demo</a>.</dd>
  2270. <dt id="addon_loadmode"><a href="../addon/mode/loadmode.js"><code>mode/loadmode.js</code></a></dt>
  2271. <dd>Defines a <code>CodeMirror.requireMode(modename,
  2272. callback)</code> function that will try to load a given mode and
  2273. call the callback when it succeeded. You'll have to
  2274. set <code>CodeMirror.modeURL</code> to a string that mode paths
  2275. can be constructed from, for
  2276. example <code>"mode/%N/%N.js"</code>—the <code>%N</code>'s will
  2277. be replaced with the mode name. Also
  2278. defines <code>CodeMirror.autoLoadMode(instance, mode)</code>,
  2279. which will ensure the given mode is loaded and cause the given
  2280. editor instance to refresh its mode when the loading
  2281. succeeded. See the <a href="../demo/loadmode.html">demo</a>.</dd>
  2282. <dt id="addon_continuecomment"><a href="../addon/comment/continuecomment.js"><code>comment/continuecomment.js</code></a></dt>
  2283. <dd>Adds a <code>continueComments</code> option, which sets whether the
  2284. editor will make the next line continue a comment when you press Enter
  2285. inside a comment block. Can be set to a boolean to enable/disable this
  2286. functionality. Set to a string, it will continue comments using a custom
  2287. shortcut. Set to an object, it will use the <code>key</code> property for
  2288. a custom shortcut and the boolean <code>continueLineComment</code>
  2289. property to determine whether single-line comments should be continued
  2290. (defaulting to <code>true</code>).</dd>
  2291. <dt id="addon_placeholder"><a href="../addon/display/placeholder.js"><code>display/placeholder.js</code></a></dt>
  2292. <dd>Adds a <code>placeholder</code> option that can be used to
  2293. make text appear in the editor when it is empty and not focused.
  2294. Also gives the editor a <code>CodeMirror-empty</code> CSS class
  2295. whenever it doesn't contain any text.
  2296. See <a href="../demo/placeholder.html">the demo</a>.</dd>
  2297. <dt id="addon_fullscreen"><a href="../addon/display/fullscreen.js"><code>display/fullscreen.js</code></a></dt>
  2298. <dd>Defines an option <code>fullScreen</code> that, when set
  2299. to <code>true</code>, will make the editor full-screen (as in,
  2300. taking up the whole browser window). Depends
  2301. on <a href="../addon/display/fullscreen.css"><code>fullscreen.css</code></a>. <a href="../demo/fullscreen.html">Demo
  2302. here</a>.</dd>
  2303. <dt id="addon_rulers"><a href="../addon/display/rulers.js"><code>display/rulers.js</code></a></dt>
  2304. <dd>Adds a <code>rulers</code> option, which can be used to show
  2305. one or more vertical rulers in the editor. The option, if
  2306. defined, should be given an array of <code>{column [, className,
  2307. color, lineStyle, width]}</code> objects or numbers (wich
  2308. indicate a column). The ruler will be displayed at the column
  2309. indicated by the number or the <code>column</code> property.
  2310. The <code>className</code> property can be used to assign a
  2311. custom style to a ruler. <a href="../demo/rulers.html">Demo
  2312. here</a>.</dd>
  2313. <dt id="addon_hardwrap"><a href="../addon/wrap/hardwrap.js"><code>wrap/hardwrap.js</code></a></dt>
  2314. <dd>Addon to perform hard line wrapping/breaking for paragraphs
  2315. of text. Adds these methods to editor instances:
  2316. <dl>
  2317. <dt><code><strong>wrapParagraph</strong>(?pos: {line, ch}, ?options: object)</code></dt>
  2318. <dd>Wraps the paragraph at the given position.
  2319. If <code>pos</code> is not given, it defaults to the cursor
  2320. position.</dd>
  2321. <dt><code><strong>wrapRange</strong>(from: {line, ch}, to: {line, ch}, ?options: object)</code></dt>
  2322. <dd>Wraps the given range as one big paragraph.</dd>
  2323. <dt><code><strong>wrapParagraphsInRange</strong>(from: {line, ch}, to: {line, ch}, ?options: object)</code></dt>
  2324. <dd>Wrapps the paragraphs in (and overlapping with) the
  2325. given range individually.</dd>
  2326. </dl>
  2327. The following options are recognized:
  2328. <dl>
  2329. <dt><code><strong>paragraphStart</strong>, <strong>paragraphEnd</strong>: RegExp</code></dt>
  2330. <dd>Blank lines are always considered paragraph boundaries.
  2331. These options can be used to specify a pattern that causes
  2332. lines to be considered the start or end of a paragraph.</dd>
  2333. <dt><code><strong>column</strong>: number</code></dt>
  2334. <dd>The column to wrap at. Defaults to 80.</dd>
  2335. <dt><code><strong>wrapOn</strong>: RegExp</code></dt>
  2336. <dd>A regular expression that matches only those
  2337. two-character strings that allow wrapping. By default, the
  2338. addon wraps on whitespace and after dash characters.</dd>
  2339. <dt><code><strong>killTrailingSpace</strong>: boolean</code></dt>
  2340. <dd>Whether trailing space caused by wrapping should be
  2341. preserved, or deleted. Defaults to true.</dd>
  2342. </dl>
  2343. A demo of the addon is available <a href="../demo/hardwrap.html">here</a>.
  2344. </dd>
  2345. <dt id="addon_merge"><a href="../addon/merge/merge.js"><code>merge/merge.js</code></a></dt>
  2346. <dd>Implements an interface for merging changes, using either a
  2347. 2-way or a 3-way view. The <code>CodeMirror.MergeView</code>
  2348. constructor takes arguments similar to
  2349. the <a href="#CodeMirror"><code>CodeMirror</code></a>
  2350. constructor, first a node to append the interface to, and then
  2351. an options object. Two extra optional options are
  2352. recognized, <code>origLeft</code> and <code>origRight</code>,
  2353. which may be strings that provide original versions of the
  2354. document, which will be shown to the left and right of the
  2355. editor in non-editable CodeMirror instances. The merge interface
  2356. will highlight changes between the editable document and the
  2357. original(s), and, unless a <code>revertButtons</code> option
  2358. of <code>false</code> is given, show buttons that allow the user
  2359. to revert changes (<a href="../demo/merge.html">demo</a>).</dd>
  2360. <dt id="addon_tern"><a href="../addon/tern/tern.js"><code>tern/tern.js</code></a></dt>
  2361. <dd>Provides integration with
  2362. the <a href="http://ternjs.net">Tern</a> JavaScript analysis
  2363. engine, for completion, definition finding, and minor
  2364. refactoring help. See the <a href="../demo/tern.html">demo</a>
  2365. for a very simple integration. For more involved scenarios, see
  2366. the comments at the top of
  2367. the <a href="../addon/tern/tern.js">addon</a> and the
  2368. implementation of the
  2369. (multi-file) <a href="http://ternjs.net/doc/demo.html">demonstration
  2370. on the Tern website</a>.</dd>
  2371. </dl>
  2372. </section>
  2373. <section id=modeapi>
  2374. <h2>Writing CodeMirror Modes</h2>
  2375. <p>Modes typically consist of a single JavaScript file. This file
  2376. defines, in the simplest case, a lexer (tokenizer) for your
  2377. language—a function that takes a character stream as input,
  2378. advances it past a token, and returns a style for that token. More
  2379. advanced modes can also handle indentation for the language.</p>
  2380. <p id="defineMode">The mode script should
  2381. call <code><strong>CodeMirror.defineMode</strong></code> to
  2382. register itself with CodeMirror. This function takes two
  2383. arguments. The first should be the name of the mode, for which you
  2384. should use a lowercase string, preferably one that is also the
  2385. name of the files that define the mode (i.e. <code>"xml"</code> is
  2386. defined in <code>xml.js</code>). The second argument should be a
  2387. function that, given a CodeMirror configuration object (the thing
  2388. passed to the <code>CodeMirror</code> function) and an optional
  2389. mode configuration object (as in
  2390. the <a href="#option_mode"><code>mode</code></a> option), returns
  2391. a mode object.</p>
  2392. <p>Typically, you should use this second argument
  2393. to <code>defineMode</code> as your module scope function (modes
  2394. should not leak anything into the global scope!), i.e. write your
  2395. whole mode inside this function.</p>
  2396. <p>The main responsibility of a mode script is <em>parsing</em>
  2397. the content of the editor. Depending on the language and the
  2398. amount of functionality desired, this can be done in really easy
  2399. or extremely complicated ways. Some parsers can be stateless,
  2400. meaning that they look at one element (<em>token</em>) of the code
  2401. at a time, with no memory of what came before. Most, however, will
  2402. need to remember something. This is done by using a <em>state
  2403. object</em>, which is an object that is always passed when
  2404. reading a token, and which can be mutated by the tokenizer.</p>
  2405. <p id="startState">Modes that use a state must define
  2406. a <code><strong>startState</strong></code> method on their mode
  2407. object. This is a function of no arguments that produces a state
  2408. object to be used at the start of a document.</p>
  2409. <p id="token">The most important part of a mode object is
  2410. its <code><strong>token</strong>(stream, state)</code> method. All
  2411. modes must define this method. It should read one token from the
  2412. stream it is given as an argument, optionally update its state,
  2413. and return a style string, or <code>null</code> for tokens that do
  2414. not have to be styled. For your styles, you are encouraged to use
  2415. the 'standard' names defined in the themes (without
  2416. the <code>cm-</code> prefix). If that fails, it is also possible
  2417. to come up with your own and write your own CSS theme file.<p>
  2418. <p id="token_style_line">A typical token string would
  2419. be <code>"variable"</code> or <code>"comment"</code>. Multiple
  2420. styles can be returned (separated by spaces), for
  2421. example <code>"string error"</code> for a thing that looks like a
  2422. string but is invalid somehow (say, missing its closing quote).
  2423. When a style is prefixed by <code>"line-"</code>
  2424. or <code>"line-background-"</code>, the style will be applied to
  2425. the whole line, analogous to what
  2426. the <a href="#addLineClass"><code>addLineClass</code></a> method
  2427. does—styling the <code>"text"</code> in the simple case, and
  2428. the <code>"background"</code> element
  2429. when <code>"line-background-"</code> is prefixed.</p>
  2430. <p id="StringStream">The stream object that's passed
  2431. to <code>token</code> encapsulates a line of code (tokens may
  2432. never span lines) and our current position in that line. It has
  2433. the following API:</p>
  2434. <dl>
  2435. <dt><code><strong>eol</strong>() → boolean</code></dt>
  2436. <dd>Returns true only if the stream is at the end of the
  2437. line.</dd>
  2438. <dt><code><strong>sol</strong>() → boolean</code></dt>
  2439. <dd>Returns true only if the stream is at the start of the
  2440. line.</dd>
  2441. <dt><code><strong>peek</strong>() → string</code></dt>
  2442. <dd>Returns the next character in the stream without advancing
  2443. it. Will return an <code>null</code> at the end of the
  2444. line.</dd>
  2445. <dt><code><strong>next</strong>() → string</code></dt>
  2446. <dd>Returns the next character in the stream and advances it.
  2447. Also returns <code>null</code> when no more characters are
  2448. available.</dd>
  2449. <dt><code><strong>eat</strong>(match: string|regexp|function(char: string) → boolean) → string</code></dt>
  2450. <dd><code>match</code> can be a character, a regular expression,
  2451. or a function that takes a character and returns a boolean. If
  2452. the next character in the stream 'matches' the given argument,
  2453. it is consumed and returned. Otherwise, <code>undefined</code>
  2454. is returned.</dd>
  2455. <dt><code><strong>eatWhile</strong>(match: string|regexp|function(char: string) → boolean) → boolean</code></dt>
  2456. <dd>Repeatedly calls <code>eat</code> with the given argument,
  2457. until it fails. Returns true if any characters were eaten.</dd>
  2458. <dt><code><strong>eatSpace</strong>() → boolean</code></dt>
  2459. <dd>Shortcut for <code>eatWhile</code> when matching
  2460. white-space.</dd>
  2461. <dt><code><strong>skipToEnd</strong>()</code></dt>
  2462. <dd>Moves the position to the end of the line.</dd>
  2463. <dt><code><strong>skipTo</strong>(ch: string) → boolean</code></dt>
  2464. <dd>Skips to the next occurrence of the given character, if
  2465. found on the current line (doesn't advance the stream if the
  2466. character does not occur on the line). Returns true if the
  2467. character was found.</dd>
  2468. <dt><code><strong>match</strong>(pattern: string, ?consume: boolean, ?caseFold: boolean) → boolean</code></dt>
  2469. <dt><code><strong>match</strong>(pattern: regexp, ?consume: boolean) → array&lt;string&gt;</code></dt>
  2470. <dd>Act like a
  2471. multi-character <code>eat</code>—if <code>consume</code> is true
  2472. or not given—or a look-ahead that doesn't update the stream
  2473. position—if it is false. <code>pattern</code> can be either a
  2474. string or a regular expression starting with <code>^</code>.
  2475. When it is a string, <code>caseFold</code> can be set to true to
  2476. make the match case-insensitive. When successfully matching a
  2477. regular expression, the returned value will be the array
  2478. returned by <code>match</code>, in case you need to extract
  2479. matched groups.</dd>
  2480. <dt><code><strong>backUp</strong>(n: integer)</code></dt>
  2481. <dd>Backs up the stream <code>n</code> characters. Backing it up
  2482. further than the start of the current token will cause things to
  2483. break, so be careful.</dd>
  2484. <dt><code><strong>column</strong>() → integer</code></dt>
  2485. <dd>Returns the column (taking into account tabs) at which the
  2486. current token starts.</dd>
  2487. <dt><code><strong>indentation</strong>() → integer</code></dt>
  2488. <dd>Tells you how far the current line has been indented, in
  2489. spaces. Corrects for tab characters.</dd>
  2490. <dt><code><strong>current</strong>() → string</code></dt>
  2491. <dd>Get the string between the start of the current token and
  2492. the current stream position.</dd>
  2493. </dl>
  2494. <p id="blankLine">By default, blank lines are simply skipped when
  2495. tokenizing a document. For languages that have significant blank
  2496. lines, you can define
  2497. a <code><strong>blankLine</strong>(state)</code> method on your
  2498. mode that will get called whenever a blank line is passed over, so
  2499. that it can update the parser state.</p>
  2500. <p id="copyState">Because state object are mutated, and CodeMirror
  2501. needs to keep valid versions of a state around so that it can
  2502. restart a parse at any line, copies must be made of state objects.
  2503. The default algorithm used is that a new state object is created,
  2504. which gets all the properties of the old object. Any properties
  2505. which hold arrays get a copy of these arrays (since arrays tend to
  2506. be used as mutable stacks). When this is not correct, for example
  2507. because a mode mutates non-array properties of its state object, a
  2508. mode object should define
  2509. a <code><strong>copyState</strong></code> method, which is given a
  2510. state and should return a safe copy of that state.</p>
  2511. <p id="indent">If you want your mode to provide smart indentation
  2512. (through the <a href="#indentLine"><code>indentLine</code></a>
  2513. method and the <code>indentAuto</code>
  2514. and <code>newlineAndIndent</code> commands, to which keys can be
  2515. <a href="#option_extraKeys">bound</a>), you must define
  2516. an <code><strong>indent</strong>(state, textAfter)</code> method
  2517. on your mode object.</p>
  2518. <p>The indentation method should inspect the given state object,
  2519. and optionally the <code>textAfter</code> string, which contains
  2520. the text on the line that is being indented, and return an
  2521. integer, the amount of spaces to indent. It should usually take
  2522. the <a href="#option_indentUnit"><code>indentUnit</code></a>
  2523. option into account. An indentation method may
  2524. return <code>CodeMirror.Pass</code> to indicate that it
  2525. could not come up with a precise indentation.</p>
  2526. <p id="mode_comment">To work well with
  2527. the <a href="#addon_comment">commenting addon</a>, a mode may
  2528. define <code><strong>lineComment</strong></code> (string that
  2529. starts a line
  2530. comment), <code><strong>blockCommentStart</strong></code>, <code><strong>blockCommentEnd</strong></code>
  2531. (strings that start and end block comments),
  2532. and <code>blockCommentLead</code> (a string to put at the start of
  2533. continued lines in a block comment). All of these are
  2534. optional.</p>
  2535. <p id="electricChars">Finally, a mode may define either
  2536. an <code>electricChars</code> or an <code>electricInput</code>
  2537. property, which are used to automatically reindent the line when
  2538. certain patterns are typed and
  2539. the <a href="#option_electricChars"><code>electricChars</code></a>
  2540. option is enabled. <code>electricChars</code> may be a string, and
  2541. will trigger a reindent whenever one of the characters in that
  2542. string are typed. Often, it is more appropriate to
  2543. use <code>electricInput</code>, which should hold a regular
  2544. expression, and will trigger indentation when the part of the
  2545. line <em>before</em> the cursor matches the expression. It should
  2546. usually end with a <code>$</code> character, so that it only
  2547. matches when the indentation-changing pattern was just typed, not when something was
  2548. typed after the pattern.</p>
  2549. <p>So, to summarize, a mode <em>must</em> provide
  2550. a <code>token</code> method, and it <em>may</em>
  2551. provide <code>startState</code>, <code>copyState</code>,
  2552. and <code>indent</code> methods. For an example of a trivial mode,
  2553. see the <a href="../mode/diff/diff.js">diff mode</a>, for a more
  2554. involved example, see the <a href="../mode/clike/clike.js">C-like
  2555. mode</a>.</p>
  2556. <p>Sometimes, it is useful for modes to <em>nest</em>—to have one
  2557. mode delegate work to another mode. An example of this kind of
  2558. mode is the <a href="../mode/htmlmixed/htmlmixed.js">mixed-mode HTML
  2559. mode</a>. To implement such nesting, it is usually necessary to
  2560. create mode objects and copy states yourself. To create a mode
  2561. object, there are <code>CodeMirror.getMode(options,
  2562. parserConfig)</code>, where the first argument is a configuration
  2563. object as passed to the mode constructor function, and the second
  2564. argument is a mode specification as in
  2565. the <a href="#option_mode"><code>mode</code></a> option. To copy a
  2566. state object, call <code>CodeMirror.copyState(mode, state)</code>,
  2567. where <code>mode</code> is the mode that created the given
  2568. state.</p>
  2569. <p id="innerMode">In a nested mode, it is recommended to add an
  2570. extra method, <code><strong>innerMode</strong></code> which, given
  2571. a state object, returns a <code>{state, mode}</code> object with
  2572. the inner mode and its state for the current position. These are
  2573. used by utility scripts such as the <a href="#addon_closetag">tag
  2574. closer</a> to get context information. Use
  2575. the <code>CodeMirror.innerMode</code> helper function to, starting
  2576. from a mode and a state, recursively walk down to the innermost
  2577. mode and state.</p>
  2578. <p>To make indentation work properly in a nested parser, it is
  2579. advisable to give the <code>startState</code> method of modes that
  2580. are intended to be nested an optional argument that provides the
  2581. base indentation for the block of code. The JavaScript and CSS
  2582. parser do this, for example, to allow JavaScript and CSS code
  2583. inside the mixed-mode HTML mode to be properly indented.</p>
  2584. <p id="defineMIME">It is possible, and encouraged, to associate
  2585. your mode, or a certain configuration of your mode, with
  2586. a <a href="http://en.wikipedia.org/wiki/MIME">MIME</a> type. For
  2587. example, the JavaScript mode associates itself
  2588. with <code>text/javascript</code>, and its JSON variant
  2589. with <code>application/json</code>. To do this,
  2590. call <code><strong>CodeMirror.defineMIME</strong>(mime,
  2591. modeSpec)</code>, where <code>modeSpec</code> can be a string or
  2592. object specifying a mode, as in
  2593. the <a href="#option_mode"><code>mode</code></a> option.</p>
  2594. <p>If a mode specification wants to add some properties to the
  2595. resulting mode object, typically for use
  2596. with <a href="#getHelpers"><code>getHelpers</code></a>, it may
  2597. contain a <code>modeProps</code> property, which holds an object.
  2598. This object's properties will be copied to the actual mode
  2599. object.</p>
  2600. <p id="extendMode">Sometimes, it is useful to add or override mode
  2601. object properties from external code.
  2602. The <code><strong>CodeMirror.extendMode</strong></code> function
  2603. can be used to add properties to mode objects produced for a
  2604. specific mode. Its first argument is the name of the mode, its
  2605. second an object that specifies the properties that should be
  2606. added. This is mostly useful to add utilities that can later be
  2607. looked up through <a href="#getMode"><code>getMode</code></a>.</p>
  2608. </section>
  2609. </article>
  2610. <script>setTimeout(function(){CodeMirror.colorize();}, 20);</script>