manual.html 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head>
  3. <title>CodeMirror user manual</title>
  4. <link rel="stylesheet" type="text/css" href="css/docs.css"/>
  5. </head>
  6. <body>
  7. <h1 class="underline">CodeMirror user manual</h1>
  8. <h2>Contents</h2>
  9. <ul>
  10. <li><a href="#useage">Basic Useage</a></li>
  11. <li><a href="#configuration">Configuration</a></li>
  12. <li><a href="#parsers">Parsers</a></li>
  13. <li><a href="#programming">Programming Interface</a></li>
  14. <li><a href="#writeparser">Writing a Parser</a></li>
  15. </ul>
  16. <h2 id="useage">Basic Usage</h2>
  17. <p>Inside the editor, the tab key is used to re-indent the current
  18. selection (or the current line when nothing is selected), and
  19. pressing enter will, apart from inserting a line break,
  20. automatically indent the new line. Pressing control-enter will
  21. cause the whole buffer to be re-coloured, which can be helpful
  22. when some colouring has become out-of-date without the editor
  23. noticing it.</p>
  24. <p>The editor sports an undo/redo system, accessible with
  25. control-z (undo) and control-y (redo). Safari will not allow
  26. client scripts to capture control-z presses, but you can use
  27. control-backspace instead on that browser.</p>
  28. <p>The key-combination control-[ triggers a paren-blink: If the
  29. cursor is directly after a '(', ')', '[', ']', '{', or '}', the
  30. editor looks for the matching character, and highlights these
  31. characters for a moment. There is an option to enable this to
  32. happen any time the user types something or moves the cursor.</p>
  33. <p>To use CodeMirror in a document, you should add a script tag to
  34. load <a href="js/codemirror.js"><code>codemirror.js</code></a>. This
  35. adds two objects to your environment, <code>CodeMirror</code> and
  36. <code>CodeMirrorConfig</code>. The first is the interface to the
  37. editor, the second can be used to configure it. (Note that this is
  38. the only name-space pollution you can expect from CodeMirror --
  39. all other cruft is kept inside the IFRAMEs that it creates when
  40. you open an editor.)</p>
  41. <p>To add an editor to a document, you must choose a place, a
  42. parser, and a style-sheet for it. For example, to append an
  43. XML editor to the body of the document, you do:</p>
  44. <pre class="code">var editor = new CodeMirror(document.body, {
  45. parserfile: "parsexml.js",
  46. stylesheet: "xmlcolors.css"
  47. });</pre>
  48. <p>The first argument to the <code>CodeMirror</code> constructor
  49. can be a DOM node, in which case the editor gets appended to that
  50. node, or a function, which will be called with the IFRAME node as
  51. argument, and which is expected to place that node somewhere in
  52. the document.</p>
  53. <p>The second (optional) argument is an object that specifies
  54. options. A set of default options (see below) is present in the
  55. <code>CodeMirrorConfig</code> object, but each instance of the
  56. editor can be given a set of specific options to override these
  57. defaults. In this case, we specified that the parser should be
  58. loaded from the <a
  59. href="js/parsexml.js"><code>"parsexml.js"</code></a> file, and
  60. that <a href="css/xmlcolors.css"><code>"xmlcolors.css"</code></a>
  61. should be used to specify the colours of the code.</p>
  62. <p>Another example:</p>
  63. <pre class="code">var editor = new CodeMirror(CodeMirror.replace("inputfield"), {
  64. parserfile: ["tokenizejavascript.js", "parsejavascript.js"],
  65. path: "lib/codemirror/js/",
  66. stylesheet: "lib/codemirror/css/jscolors.css",
  67. content: document.getElementById("inputfield").value
  68. });</pre>
  69. <p>Here we use the utility function
  70. <code>CodeMirror.replace</code> to create a function that will
  71. replace a node in the current document (given either directly or
  72. by ID) with the editor. We also select the JavaScript parser this
  73. time, and give a <code>path</code> option to tell the editor that
  74. its files are not located in the same directory as the current
  75. HTML page, but in <code>"lib/codemirror/"</code>.</p>
  76. <p>There is a function
  77. <code>CodeMirror.isProbablySupported()</code> that causes some
  78. 1998-style browser detection to happen, returning
  79. <code>false</code> if CodeMirror is probably not supported on the
  80. browser, <code>true</code> if it probably is, and
  81. <code>null</code> if it has no idea. As the name suggests, this is
  82. not something you can rely on, but it's usually better than
  83. nothing.</p>
  84. <p>Another utility function, <code>CodeMirror.fromTextArea</code>,
  85. will, given a textarea node or the id of such a node, hide the
  86. textarea and replace it with a CodeMirror frame. If the textarea
  87. was part of a form, an <code>onsubmit</code> handler will be
  88. registered with this form, which will load the content of the
  89. editor into the textarea, so that it can be submitted as normal.
  90. This function optionally takes a configuration object as second
  91. argument.</p>
  92. <pre class="code">var editor = CodeMirror.fromTextArea("inputfield", {
  93. parserfile: ["tokenizejavascript.js", "parsejavascript.js"],
  94. path: "lib/codemirror/js/",
  95. stylesheet: "lib/codemirror/css/jscolors.css"
  96. });</pre>
  97. <p>Instances created like this will have a
  98. <code>toTextArea()</code> method which removes them and restores
  99. the text area they were based on.</p>
  100. <p>The reason that the script path has to be configured is that
  101. CodeMirror will load in a bunch of extra files when an editor is
  102. created (the parser script, among others). To be able to do this,
  103. it has to know where to find them. These are all the JavaScript
  104. files that are part of CodeMirror itself:</p>
  105. <dl>
  106. <dt><a href="js/codemirror.js"><code>codemirror.js</code></a></dt>
  107. <dd>Main interface, takes care of default configuration and the
  108. definition of editor frames. Include this in your HTML
  109. document.</dd>
  110. <dt><a href="js/editor.js"><code>editor.js</code></a></dt> <dd>The
  111. code that takes care of reacting to user input, colouring text,
  112. and indenting lines.</dd>
  113. <dt><a href="js/util.js"><code>util.js</code></a></dt> <dd>A few
  114. generic utility functions.</dd>
  115. <dt><a
  116. href="js/undo.js"><code>undo.js</code></a></dt>
  117. <dd>Implements the undo history for the editor.</dd>
  118. <dt><a
  119. href="js/stringstream.js"><code>stringstream.js</code></a></dt>
  120. <dd>Objects for wrapping the textual input to the parser.</dd>
  121. <dt><a href="js/select.js"><code>select.js</code></a></dt> <dd>Some
  122. helper utilities for working with selected text and cursor
  123. positions.</dd>
  124. <dt><a href="js/tokenize.js"><code>tokenize.js</code></a></dt>
  125. <dd>Helper framework for writing tokenisers.</dd>
  126. </dl>
  127. <p>Most of these are rather full of comments, which can be useful
  128. when you are trying to figure out how they work, but wastes a lot
  129. of bandwidth in a production system. Take a look at the
  130. description of the <code>basefiles</code> option below if you want
  131. to concatenate and minimise the library (see the <a href="compress.html">compression API</a>).</p>
  132. <p>Apart from these, there are files that implement the various
  133. parsers. These all start with either <code>parse</code> or
  134. <code>tokenize</code>.</p>
  135. <h2 id="configuration">Configuration</h2>
  136. <p>There are three ways to configure CodeMirror:</p>
  137. <ul>
  138. <li>If you define a global <code>CodeMirrorConfig</code> object
  139. before loading <a
  140. href="js/codemirror.js"><code>codemirror.js</code></a>, the
  141. configuration options in that object will override the
  142. defaults.</li>
  143. <li>By assigning to the properties of the
  144. <code>CodeMirrorConfig</code> object, configuration defaults can
  145. be overridden after loading <a
  146. href="js/codemirror.js"><code>codemirror.js</code></a>.</li>
  147. <li>The <code>CodeMirror</code> constructor can be given a second
  148. argument, an object, which will override some options for just
  149. that editor. Options not mentioned in this object will default to
  150. the values in the <code>CodeMirrorConfig</code> object.</li>
  151. </ul>
  152. <p>The options that can be specified are these (most have sensible
  153. defaults specified in <a
  154. href="js/codemirror.js"><code>codemirror.js</code></a>):</p>
  155. <dl>
  156. <dt><code>stylesheet</code></dt><dd>The file name of the
  157. style-sheet, or style-sheets, that should be used to colour the
  158. code in the editor frame. Can be a string or an array of
  159. strings. See <a
  160. href="css/jscolors.css"><code>jscolors.css</code></a> for an
  161. example. The set of active stylesheets can be changed in a
  162. running editor with the <code>setStylesheet</code> method, which
  163. also takes a string or array of strings as argument.</dd>
  164. <dt><code>path</code></dt><dd>The path that is prefixed to
  165. script file names when they are loaded into an IFRAME. (Note that
  166. this is not applied to the style-sheet file name.)</dd>
  167. <dt><code>parserfile</code></dt><dd>A file name string, or an
  168. array of strings that name the files containing the parser. See
  169. below for the interface that such a parser should
  170. implement.</dd>
  171. <dt><code>basefiles</code></dt><dd>An array of strings naming
  172. the files containing the base CodeMirror functionality. Defaults
  173. to <code>["util.js", "stringstream.js", "select.js", "undo.js",
  174. "editor.js", "tokenize.js"]</code>, but if you put them all into
  175. a single file to reduce latency, or add some functionality, you
  176. might have to adjust that.</dd>
  177. <dt><code>iframeClass</code></dt><dd>Set this to a string to
  178. give the IFRAME node created for the editor a custom CSS class.
  179. Defaults to <code>null</code>.</dd>
  180. <dt><code>passDelay</code></dt><dd>Gives the amount of
  181. milliseconds between colouring passes. Defaults to 200.</dd>
  182. <dt><code>passTime</code></dt><dd>Specifies the maximum amount
  183. of time that the highlighter will spend in one shot. Setting
  184. this too high will cause the editor to 'freeze' the browser for
  185. noticeable intervals. Defaults to 50.</dd>
  186. <dt><code>continuousScanning</code></dt><dd>Configure continuous
  187. scanning of the document. When <code>false</code>, scanning is
  188. disabled. When set to a number, say <code>N</code>, a
  189. 'background' process will scan the document for
  190. <code>passTime</code> (see above) milliseconds every
  191. <code>N</code> milliseconds, regardless of whether anything
  192. changed. This makes sure non-local changes propagate through the
  193. document, and will help keep everything consistent. It does add
  194. extra processing cost, even for an idle editor. Default value is
  195. <code>false</code>.</dd>
  196. <dt><code>autoMatchParens</code></dt><dd>When <code>true</code>,
  197. will cause parens to be matched every time a key is pressed or
  198. the user clicks on the document. Defaults to <code>false</code>.
  199. Might be expensive for big documents.</dd>
  200. <dt><code>markParen</code>,
  201. <code>unmarkParen</code></dt><dd>Functions. Can be used to
  202. customise the way brackets are marked (and unmarked) when
  203. matched. Both take the bracket's <code>SPAN</code> node as their
  204. first argument, and <code>markParen</code> takes a second
  205. boolean argument indicating whether a successful match was
  206. found. The default is to make the brackets bold and green (or
  207. red, if not matched).</dd>
  208. <dt><code>saveFunction</code></dt><dd>If given a function
  209. value, that function will be invoked when the user presses
  210. control-s. You should advise your Opera users to use
  211. control-shift-s instead, since plain control-s will bring up the
  212. 'save page' dialog. Defaults to <code>null</code>.</dd>
  213. <dt><code>undoDepth</code></dt><dd>Maximum length of the undo
  214. history. Default is 50. This setting is changeable with the
  215. <code>setUndoDepth(depth)</code> method on CodeMirror
  216. instances.</dd>
  217. <dt><code>onChange</code></dt><dd>An optional function of zero
  218. arguments that gets called whenever the document is changed.
  219. Happens at undo-commit time, not instantaniously.</dd>
  220. <dt><code>undoDelay</code></dt><dd>When nothing is done in the
  221. editor for this amount of milliseconds, pending changes get
  222. added to the undo history. Setting this lower will give the undo
  223. functionality a finer granularity. Defaults to 800.</dd>
  224. <dt><code>width</code>, <code>height</code></dt><dd>The size of
  225. the editor frame, given as a style-sheet quantities (for example
  226. <code>"600px"</code> or <code>"100%"</code>). When
  227. <code>height</code> is set to <code>dynamic</code>, the editor
  228. will automatically resize to fit its contents. In this case, the
  229. <code>minHeight</code> option (an integer) is used to determine
  230. the minimum height in pixels.</dd>
  231. <dt><code>disableSpellcheck</code></dt><dd>Should the editor
  232. disable spell-checking on browsers that support it (Firefox 2+).
  233. Default is <code>true</code>, since for most code spell-checking
  234. is useless. Can be changed with the
  235. <code>setSpellCheck(on)</code> method.</dd>
  236. <dt><code>textWrapping</code></dt><dd>Can be used to disable or
  237. enable text-wrapping in the editor frame. Default is
  238. <code>true</code>. Changeable with the
  239. <code>setTextWrapping(on)</code> method.</dd>
  240. <dt><code>lineNumbers</code></dt><dd>Show line numbers to the
  241. left of the editor. This requires you to specify a style for the
  242. <code>CodeMirror-line-numbers</code> CSS class (in the outer
  243. document) to configure the width, font, colors, etcetera for the
  244. line-number DIV. You have to make sure that lines in the
  245. numbering element have the same height as lines in the editor.
  246. This is most easily done by giving them both the same font and
  247. an absolute ('pt' or 'px') font size. This option defaults to
  248. <code>false</code>. Changeable with the
  249. <code>setLineNumbers(on)</code> method.</dd>
  250. <dt><code>lineNumberDelay</code>,
  251. <code>lineNumberTime</code></dt><dd>When both line numbers are
  252. and text wrapping are turned on, updating line numbers can be
  253. expensive. These settings can be used to control how fast the
  254. updating happens &#x2015; they work in the same way as
  255. <code>passDelay</code> and <code>passTime</code>.</dd>
  256. <dt><code>indentUnit</code></dt><dd>An integer that specifies
  257. the amount of spaces one 'level' of indentation should add.
  258. Default is <code>2</code>. Changeable in a running editor using
  259. the <code>setIndentUnit(spaces)</code> method.</dd>
  260. <dt><code>tabMode</code></dt><dd>Determines what the effect of
  261. pressing tab is. Possibilities are:
  262. <dl>
  263. <dt><code>"indent"</code></dt><dd>The default. Causes tab to
  264. adjust the indentation of the selection or current line using
  265. the parser's rules.</dd>
  266. <dt><code>"spaces"</code></dt><dd>Pressing tab simply inserts
  267. four spaces.</dd>
  268. <dt><code>"default"</code></dt><dd>CodeMirror does not
  269. interfere with the tab key, but leaves it to the browser to
  270. handle it. Binds shift-space to regular indentation
  271. behaviour.</dd>
  272. <dt><code>"shift"</code></dt><dd>Pressing tab indents the
  273. current line (or selection) one <code>indentUnit</code>
  274. deeper, pressing shift-tab, un-indents it.</dd>
  275. </dl>
  276. This option can be changed at run-time using the
  277. <code>setTabMode(mode)</code> method.</dd>
  278. <dt><code>reindentOnLoad</code></dt><dd>When <code>true</code>,
  279. this causes the content of the editor to be reindented
  280. immediately when the editor loads. Defaults to
  281. <code>false</code>.</dd>
  282. <dt><code>readOnly</code></dt><dd>When set to <code>true</code>,
  283. the document is not editable.</dd>
  284. <dt><code>domain</code></dt><dd>Can be set to the value
  285. <code>document.domain</code> should have inside of the iframe.
  286. Used to prevent access issues in IE, where this value isn't
  287. automatically inherited by iframes.</dd>
  288. <dt><code>initCallback</code></dt><dd>If set to a function, this
  289. will be called (with the editor object as its argument) after
  290. the editor has finished initialising.</dd>
  291. <dt><code>cursorActivity</code></dt><dd>A function that is
  292. called every time the cursor moves, with the top-level node that
  293. the cursor is inside or next to as an argument. Can be used to
  294. have some controls react to the context of the cursor.</dd>
  295. <dt><code>activeTokens</code></dt><dd>Can be set to a function
  296. that will be called with <code>(spanNode, tokenObject,
  297. editor)</code> arguments whenever a new token node is being
  298. added to the document. Can be used to do things like add event
  299. handlers to nodes. Should <em>not</em> change the DOM structure
  300. of the node (so no turning the span into a link), since this
  301. will greatly confuse the editor.</dd>
  302. <dt id="parserConfig"><code>parserConfig</code></dt><dd>An
  303. object value that is passed along to the parser to configure it.
  304. What this object should look like depends on the parser
  305. used.</dd>
  306. <dt><code>content</code></dt><dd>The starting content of the
  307. editor. You'll probably not want to provide a global default for
  308. this, but add it to the <code>options</code> object passed to
  309. individual editors as they are created.</dd>
  310. </dl>
  311. <h2 id="parsers">Parsers</h2>
  312. <p>(If you want to use a CodeMirror parser to highlight a piece of
  313. text, without creating an editor, see <a
  314. href="highlight.html">this example</a>, and the <code><a
  315. href="js/highlight.js">highlight.js</a></code> script.)</p>
  316. <p>The following parsers come with the distribution of CodeMirror:</p>
  317. <dl>
  318. <dt><code><a href="js/parsexml.js">parsexml.js</a></code> (<a
  319. href="htmltest.html">demo</a>)</dt><dd>A HTML/XML parser. Takes
  320. a <code>useHTMLKludges</code> configuration option (see the
  321. <code><a href="#parserConfig">parserConfig</a></code> option
  322. above), which specifies whether the content of the editor is
  323. HTML or XML, and things like self-closing tags (<code>br</code>,
  324. <code>img</code>) exist. This defaults to <code>true</code>.
  325. Example colours for the styles that this parser uses are defined
  326. in <code><a
  327. href="css/xmlcolors.css">css/xmlcolors.css</a></code>.</dd>
  328. <dt><code><a
  329. href="js/tokenizejavascript.js">tokenizejavascript.js</a></code>,
  330. <code><a
  331. href="js/parsejavascript.js">parseejavascript.js</a></code> (<a
  332. href="jstest.html">demo</a>)</dt><dd>The JavaScript parser.
  333. Example colours in <code><a
  334. href="css/jscolors.css">css/jscolors.css</a></code>. Takes one
  335. configuration option, <code>json</code>, that can be set when
  336. the editor content is JSON data. It causes every opening brace
  337. to be treated as the start of an object, rather than a
  338. block.</dd>
  339. <dt><code><a href="js/parsecss.js">parsecss.js</a></code> (<a
  340. href="csstest.html">demo</a>)</dt><dd>A CSS parser. Styles in
  341. <code><a
  342. href="css/csscolors.css">css/csscolors.css</a></code></dd>
  343. <dt><code><a
  344. href="js/parsehtmlmixed.js">parsehtmlmixed.js</a></code> (<a
  345. href="mixedtest.html">demo</a>)</dt><dd>A mixed-mode HTML
  346. parser. Requires the XML, JavaScript, and CSS parsers to also be
  347. loaded, so your <code>parserfile</code> option looks something
  348. like <code>["parsexml.js", "parsecss.js",
  349. "tokenizejavascript.js", "parsejavascript.js",
  350. "parsehtmlmixed.js"]</code>.</dd>
  351. <dt><code><a href="js/parsesparql.js">parsesparql.js</a></code>
  352. (<a href="sparqltest.html">demo</a>)</dt><dd>Parses the <a
  353. href="http://en.wikipedia.org/wiki/SPARQL">SPARQL</a> query
  354. language. Example styles in <code><a
  355. href="css/sparqlcolors.css">css/sparqlcolors.css</a></code></dd>
  356. <dt><code><a
  357. href="js/parsedummy.js">parsedummy.js</a></code></dt><dd>A
  358. 'dummy' parser to make it possible to edit plain text, or
  359. documents for which no suitable parser exists.</dd>
  360. <dt><code><a
  361. href="contrib/php/js/parsephp.js">contrib/php/js/parsephp.js</a></code>
  362. (<a href="contrib/php/index.html">demo</a>)</dt><dd>PHP parser.
  363. There is also <code><a
  364. href="contrib/php/js/parsephphtmlmixed.js">contrib/php/js/parsephphtmlmixed.js</a></code>,
  365. which wraps this parser to allow mixed-mode HTML + PHP parsing.
  366. This one takes a configuration parameter <code>opening</code>,
  367. which should contain an array of XML processing instruction
  368. openings (<code>&lt;?php</code>, <code>&lt;?</code> etc) which
  369. can be used to open a PHP block. Default is
  370. <code>["&lt;?php"]</code>.</dd>
  371. <dt><code><a
  372. href="contrib/python/js/parsepython.js">contrib/python/js/parsepython.js</a></code>
  373. (<a href="contrib/python/index.html">demo</a>)</dt><dd>Python
  374. parser.</dd>
  375. <dt><code><a href="contrib/lua/js/parselua.js">contrib/lua/js/parselua.js</a></code>
  376. (<a href="contrib/lua/index.html">demo</a>)</dt><dd>Lua
  377. parser.</dd>
  378. </dl>
  379. <h2 id="programming">Programming Interface</h2>
  380. <p>To be as flexible as possible, CodeMirror implements a very
  381. plain editable field, without any accompanying buttons, bells, and
  382. whistles. <code>CodeMirror</code> objects do, however, provide a
  383. number of methods and properties that make it possible to add
  384. extra functionality around the editor. <a
  385. href="js/mirrorframe.js"><code>mirrorframe.js</code></a> provides
  386. a basic example of their usage.</p>
  387. <h3>Properties</h3>
  388. <dl>
  389. <dt><code>frame</code></dt><dd>The editable frame.</dd>
  390. <dt><code>win</code></dt><dd>The window of the editable frame.
  391. Mostly useful for attaching event handlers.</dd>
  392. <dt><code>wrapping</code></dt><dd>The <code>DIV</code> element
  393. wrapped around the frame. This always has a CSS class of
  394. <code>CodeMirror-wrapping</code>.</dd>
  395. </dl>
  396. <h3>Methods</h3>
  397. <dl>
  398. <dt><code>getCode()</code> &#8594;
  399. <code>string</code></dt><dd>Returns the current content of the
  400. editor, as a string.</dd>
  401. <dt><code>setCode(string)</code></dt><dd>Replaces the current
  402. content of the editor with the given value.</dd>
  403. <dt><code>focus()</code></dt><dd>Gives focus to the editor
  404. frame.</dd>
  405. <dt><code>selection()</code> &#8594;
  406. <code>string</code></dt><dd>Returns the text that is currently
  407. selected in the editor.</dd>
  408. <dt><code>replaceSelection(string)</code></dt><dd>Replaces the
  409. currently selected text with the given string. Will also cause
  410. the editor frame to gain focus.</dd>
  411. <dt><code>reindent()</code></dt><dd>Automatically re-indent the
  412. whole document.</dd>
  413. <dt><code>reindentSelection()</code></dt><dd>Automatically
  414. re-indent the selected lines.</dd>
  415. <dt><code>getSearchCursor(string, atCursor, caseFold)</code>
  416. &#8594; <code>cursor</code></dt><dd>The first argument indicates
  417. the string that should be searched for, and the second indicates
  418. whether searching should start at the cursor (<code>true</code>)
  419. or at the start of the document (<code>false</code>). When
  420. <code>caseFold</code> is true, the search will be
  421. case-insensitive; if <code>caseFold</code> is unspecified, the
  422. search will be case-sensitive if the search string contains
  423. uppercase characters. Returns an object that provides an interface
  424. for searching. Call its <code>findNext()</code> method to search
  425. for an occurrence of the given string. This returns
  426. <code>true</code> if something is found, or <code>false</code>
  427. if the end of document is reached. When an occurrence has been
  428. found, you can call <code>select()</code> to select it, or
  429. <code>replace(string)</code> to replace it with a given string.
  430. Note that letting the user change the document, or
  431. programmatically changing it in any way except for calling
  432. <code>replace</code> on the cursor itself, might cause a cursor
  433. object to skip back to the beginning of the document.</dd>
  434. <dt><code>undo()</code></dt><dd>Undo one changeset, if available.</dd>
  435. <dt><code>redo()</code></dt><dd>Redo one changeset, if available.</dd>
  436. <dt><code>historySize() &#8594; object</code></dt><dd>Get a
  437. <code>{undo, redo}</code> object holding the sizes of the undo
  438. and redo histories.</dd>
  439. <dt><code>clearHistory()</code></dt><dd>Drop all history
  440. information.</dd>
  441. <dt><code>grabKeys(callback, filter)</code></dt><dd>Route
  442. keyboard input in the editor to a callback function. This
  443. function is given a slightly normalised (see
  444. <code>normalizeEvent</code> in <a
  445. href="js/util.js"><code>util.js</code></a>) <code>keydown</code>
  446. event object. If a second argument is given, this will be used
  447. to determine which events to apply the callback to. It should
  448. take a key code (as in <code>event.keyCode</code>), and return a
  449. boolean, where <code>true</code> means the event should be
  450. routed to the callback, and <code>false</code> leaves the key to
  451. perform its normal behaviour.</dd>
  452. <dt><code>ungrabKeys()</code></dt><dd>Revert the effect of
  453. <code>grabKeys</code>.</dd>
  454. <dt><code>setParser(name, parserConfig)</code></dt><dd>Change
  455. the active parser. To use this you'll have to load more than one
  456. parser (put the one you want to use as default at the end of the
  457. list). Then call this function with a string containing the name
  458. of the parser you want to switch to (see the parser script file
  459. to find the name, it'll be something like
  460. <code>CSSParser</code>). The second argument is optional, and
  461. can be used to pass a new parser configuration object.</dd>
  462. <dt><code>cursorCoords(start)</code></dt><dd>Get the coordinates
  463. of the cursor in the editor, relative to the top-left corner of
  464. the outer document. Normally returns an object with
  465. <code>x</code>, <code>y</code>, and <code>yBot</code> (the
  466. bottom of the cursor) properties. May return <code>null</code>
  467. if the cursor position could not be determined (for example, if
  468. the editor is not focused).</dd>
  469. </dl>
  470. <p>For detailed interaction with the content of the editor,
  471. CodeMirror exposes a line-oriented interface, which allows you to
  472. inspect and manipulate the document line by line. Line handles
  473. should be considered opaque (they are in fact the <code>BR</code>
  474. nodes at the start of the line), except that the value
  475. <code>false</code> (but <em>not</em> <code>null</code>) always
  476. denotes an invalid value. Since changing the document might cause
  477. some line handles to become invalid, every function that takes
  478. them as argument can throw
  479. <code>CodeMirror.InvalidLineHandle</code>. These are the relevant
  480. methods:</p>
  481. <dl>
  482. <dt><code>cursorPosition(start)</code> &#8594;
  483. <code>object</code></dt><dd>Retrieve a <code>{line,
  484. character}</code> object representing the cursor position.
  485. <code>start</code> defaults to <code>true</code> and determines
  486. if the startpoint or the endpoint of the selection is used.</dd>
  487. <dt><code>cursorLine()</code> &#8594;
  488. <code>handle</code></dt><dd>Returns the line on which the cursor
  489. is currently sitting.</dd>
  490. <dt><code>firstLine()</code> &#8594;
  491. <code>handle</code></dt><dd>Get the first line of the
  492. document.</dd>
  493. <dt><code>lastLine()</code> &#8594;
  494. <code>handle</code></dt><dd>The last line.</dd>
  495. <dt><code>nextLine(handle)</code> &#8594;
  496. <code>handle</code></dt><dd>Get the line after the given one, or
  497. <code>false</code> if that was the last line.</dd>
  498. <dt><code>prevLine(handle)</code> &#8594;
  499. <code>handle</code></dt><dd>Find the line before the given one,
  500. return <code>false</code> if that was the first line.</dd>
  501. <dt><code>nthLine(number)</code> &#8594;
  502. <code>handle</code></dt><dd>Find the Nth line of the document.
  503. Note that the first line counts as one, not zero. Returns
  504. <code>false</code> if there is no such line.</dd>
  505. <dt><code>lineContent(handle)</code> &#8594;
  506. <code>string</code></dt><dd>Retrieve the content of the
  507. line.</dd>
  508. <dt><code>setLineContent(handle, string)</code></dt><dd>Replace
  509. the content of the line with the given string.</dd>
  510. <dt><code>removeLine(handle)</code></dt><dd>Remove the given
  511. line from the editor. The handle will stay valid after this
  512. operation, and now refers to the next line.</dd>
  513. <dt><code>lineNumber(handle)</code> &#8594;
  514. <code>number</code></dt><dd>Ask which line of the document
  515. (1-based) the given line is.</dd>
  516. <dt><code>jumpToLine(handle)</code></dt><dd>Moves the cursor to
  517. the start of the given line.</dd>
  518. <dt><code>selectLines(startHandle, startOffset,
  519. endHandle, endOffset)</code></dt><dd>Move the selection to a
  520. specific point. <code>endHandle</code> and
  521. <code>endOffset</code> can be omitted to just place the cursor
  522. somewhere without selecting any text.</dd>
  523. <dt><code>insertIntoLine(handle, position,
  524. text)</code></dt><dd>Insert a piece of text into a line.
  525. <code>position</code> can be an integer, specifying the position
  526. in the line where the text should be inserted, or the string
  527. <code>"end"</code>, for the end of the line.</dd>
  528. </dl>
  529. <h2 id="writeparser">Writing a Parser</h2>
  530. <p>A parser is implemented by one or more files (see
  531. <code>parserfile</code> above) which, when loaded, add a
  532. <code>Parser</code> property to the <code>Editor</code> object
  533. defined by <a href="js/editor.js"><code>editor.js</code></a>. This
  534. object must support the following interface:</p>
  535. <dl>
  536. <dt><code>make(stream)</code></dt><dd>A function that, given a
  537. string stream (see <a
  538. href="js/stringstream.js"><code>stringstream.js</code></a>),
  539. creates a parser. The behaviour of this parser is described
  540. below.</dd>
  541. <dt><code>electricChars</code></dt><dd>An optional string
  542. containing the characters that, when typed, should cause the
  543. indentation of the current line to be recomputed (for example
  544. <code>"{}"</code> for c-like languages).</dd>
  545. <dt><code>configure(object)</code></dt><dd>An optional function
  546. that can be used to configure the parser. If it exists, and an
  547. editor is given a <code>parserConfig</code> option, it will be
  548. called with the value of that option.</dd>
  549. <dt><code>firstIndentation(chars, current,
  550. direction)</code></dt><dd>An optional function that is used to
  551. determine the proper indentation of the first line of a
  552. document. When not provided, <code>0</code> is used.</dd>
  553. </dl>
  554. <p>When the <code>make</code> method is called with a string
  555. stream, it should return a MochiKit-style iterator: an object with
  556. a <code>next</code> method, which will raise
  557. <code>StopIteration</code> when it is at its end (see <a
  558. href="http://bob.pythonmac.org/archives/2005/07/06/iteration-in-javascript/">this</a>
  559. for details). This iterator, when called, will consume input from
  560. the string stream, and produce a token object.</p>
  561. <p>Token objects represent a single significant piece of the text
  562. that is being edited. A token object must have a
  563. <code>value</code> property holding the text it stands for, and a
  564. <code>style</code> property with the CSS class that should be used
  565. to colour this element. This can be anything, except that any
  566. whitespace at the start of a line should <em>always</em> have
  567. class <code>"whitespace"</code>: The editor must be able to
  568. recognize these when it indents lines. Furthermore, each newline
  569. character <em>must</em> have its own separate token, which has an
  570. <code>indentation</code> property holding a function that can be
  571. used to determine the proper indentation level for the next line.
  572. This function optionally takes the text in the first token of the
  573. next line, the current indentation of the line, and the
  574. 'direction' of the indentation as arguments, which it can use to
  575. adjust the indentation level. The direction argument is only
  576. useful for modes in which lines do not have a fixed indentation,
  577. and can be modified by multiple tab presses. It is
  578. <code>null</code> for 'default' indentations (like what happens
  579. when the user presses enter), <code>true</code> for regular tab
  580. presses, and <code>false</code> for control-tab or shift-tab.</p>
  581. <p>So far this should be pretty easy. The hard part is that this
  582. iterator must also have a <code>copy</code> method. This method,
  583. called without arguments, returns a function representing the
  584. current state of the parser. When this state function is later
  585. called with a string stream as its argument, it returns a parser
  586. object that resumes parsing using the old state and the new input
  587. stream. It may assume that only one parser is active at a time,
  588. and can clobber the state of the old parser if it wants.</p>
  589. <p>For examples, see <a
  590. href="js/parsejavascript.js"><code>parsejavascript.js</code></a>,
  591. <a href="js/parsexml.js"><code>parsexml.js</code></a>, and <a
  592. href="js/parsecss.js"><code>parsecss.js</code></a>.</p>
  593. </body>
  594. </html>