Last active
August 29, 2015 14:21
-
-
Save mhuebert/7a84a0773733937e20b3 to your computer and use it in GitHub Desktop.
Clojure-Markdown CodeMirror mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've currently got this working reasonably well - with paredit &matching brackets - but indentation doesn't work as it does in plain clojure mode.