You can determine which tiddler content types are edited by the ~CodeMirror widget by creating or modifying special tiddlers whose prefix is comprised of the string `$:/config/EditorTypeMappings/` concatenated with the content type. The text of that tiddler gives the editor type to be used (eg, ''text'', ''bitmap'', ''codemirror'').
The current editor type mappings are shown in [[$:/ControlPanel]] under the "Advanced" tab.
! ~CodeMirror Configuration
You can configure the ~CodeMirror plugin by creating a tiddler called [[$:/config/CodeMirror]] containing a JSON configuration object. The configuration tiddler must have its type field set to `application/json` to take effect.
See http://codemirror.net/ for details of available configuration options.
# You should see line numbers when editing a tiddler
# When editing a tiddler, no matter what the type of the tiddler is set to, you should see matching brackets being highlighted whenever the cursor is next to one of them
# If you edit a tiddler with the type `application/javascript` or `application/json` you should see the code being syntax highlighted
!! Add HTML syntax highlighting
# Create a tiddler `$:/plugins/tiddlywiki/codemirror/mode/xml/xml.js`
## Add a field `module-type` and set it to ''library''
## Set the field `type` to ''application/javascript''
## Set the text field of the tiddler with the javascript code from this link : [[https://raw.githubusercontent.com/codemirror/CodeMirror/master/mode/xml/xml.js]]
# Set the text field of the tiddler `$:/config/CodeMirror` to:
# Edit a tiddler with the type `text/html` and write some html code. You should see your code being coloured
!! Add a non-existing language mode
Here's an example of adding a new language mode - in this case, the language C.
# Create a tiddler `$:/plugins/tiddlywiki/codemirror/mode/clike/clike.js`
## Add a field `module-type` and set it to ''library''
## Set the field `type` to ''application/javascript''
## Set the text field of the tiddler with the javascript code from this link : [[https://raw.githubusercontent.com/codemirror/CodeMirror/master/mode/clike/clike.js]]
# Set the text field of the tiddler `$:/config/CodeMirror` to:
## Find the matching MIME type. If you go on the [[CodeMirror documentation for language modes|http://codemirror.net/mode/index.html]] you can see the [[documentation for the c-like mode|http://codemirror.net/mode/clike/index.html]]. In this documentation, at the end you will be told the MIME types defined. Here it's ''text/x-csrc''
## Add the tiddler: `$:/config/EditorTypeMappings/text/x-csrc` and fill the text field with : ''codemirror''
If you edit a tiddler with the type `text/x-csrc` and write some code in C, you should see your text being coloured.
!! Add matching tags
# Add XML and HTML colouring
# Create a tiddler `$:/plugins/tiddlywiki/codemirror/addon/edit/matchtags.js`
## Add a field `module-type` and set it to ''library''
## Set the field `type` to ''application/javascript''
## Set the text field of the tiddler with the javascript code from this link : [[http://codemirror.net/addon/edit/matchtags.js]]
# Set the text field of the tiddler `$:/config/CodeMirror` to:
Edit a tiddler that has the type :`text/htm` and write this code:
```
<html>
<div id="click here and press CTRL+J">
<ul>
<li>
</li>
</ul>
</div>
</html>
```
If you click on a tag and press CTRL+J, your cursor will select the matching tag. Supposedly, it should highlight the pair when clicking a tag. However, that part doesn't work.
!! Adding closing tags
# Add the xml mode (see "Add XML and HTML colouring")
# Create a tiddler `$:/plugins/tiddlywiki/codemirror/addon/edit/closetags.js`
## Add a field `module-type` and set it to ''library''
## Set the field `type` to ''application/javascript''
## Set the text field of the tiddler with the javascript code from this link : [[http://codemirror.net/addon/edit/closetag.js]]
# Set the text field of the tiddler `$:/config/CodeMirror` to:
# If you try to edit any tiddler and write `if(` you should see the bracket closing itself automatically (you will get "if()"). It works with (), [], and {}
# If you try and edit a tiddler with the type `application/javascript`, it will auto-close `()`,`[]`,`{}`,`''` and `""`
!! Adding folding tags
# Create a tiddler `$:/plugins/tiddlywiki/codemirror/addon/fold/foldcode.js`
## Add a field `module-type` and set it to ''library''
## Set the field `type` to ''application/javascript''
## Set the text field of the tiddler with the javascript code from this link : [[http://codemirror.net/addon/fold/foldcode.js]]
# Repeat the above process for the following tiddlers, but replace the code with the one from the given link:
## Create a tiddler `$:/plugins/tiddlywiki/codemirror/addon/fold/xml-fold.js`, the code can be found here [[https://raw.githubusercontent.com/codemirror/CodeMirror/master/addon/fold/xml-fold.js]]
## Create a tiddler `$:/plugins/tiddlywiki/codemirror/addon/fold/foldgutter.js`, the code can be found here [[http://codemirror.net/addon/fold/foldgutter.js]]
# Create a tiddler `$:/plugins/tiddlywiki/codemirror/addon/fold/foldgutter.css`