Skip to content

Instantly share code, notes, and snippets.

@mhuebert
Last active August 29, 2015 14:21
Show Gist options
  • Save mhuebert/7a84a0773733937e20b3 to your computer and use it in GitHub Desktop.
Save mhuebert/7a84a0773733937e20b3 to your computer and use it in GitHub Desktop.
Clojure-Markdown CodeMirror mode
CodeMirror.defineMode("clojure-markdown", function(options){
var clojureMode = CodeMirror.getMode({}, "clojure");
var markdownMode = CodeMirror.getMode({}, "markdown");
return {
startState: function() {
return {
mode: markdownMode,
localState: markdownMode.startState(),
depth: 0,
prevDepth: 0,
lastChar: ""
};
},
copyState: function(state) {
return {
mode: state.mode,
localState: CodeMirror.copyState(state.mode, state.localState),
depth: state.depth,
prevDepth: state.prevDepth,
lastChar: state.lastChar
}
},
innerMode: function(state) {
return {
state: state.localState,
mode: state.mode
}
},
indent: function (state) {
return state.mode.indent && state.mode.indent(state.localState)
},
token: function(stream, state) {
state.prevDepth = state.depth
var cur = stream.peek()
if (cur == "(" && state.lastChar != "\\") {state.depth += 1}
if (cur == ")" && state.lastChar != "\\") {state.depth -= 1}
state.lastChar = cur
styleDepth = Math.max(state.depth, state.prevDepth)
var newMode = styleDepth == 0 ? markdownMode : clojureMode
if (newMode !== state.mode) {
state.mode = newMode
state.localState = newMode.startState()
}
var style = state.mode.token(stream, state.localState)
return style;
}
}
})
@mhuebert
Copy link
Author

I've currently got this working reasonably well - with paredit &matching brackets - but indentation doesn't work as it does in plain clojure mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment