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