1 CodeMirror.defineMode("commonlisp", function (config) {
2 var assumeBody = /^with|^def|^do|^prog|case$|^cond$|bind$|when$|unless$/;
3 var numLiteral = /^(?:[+\-]?(?:\d+|\d*\.\d+)(?:[efd][+\-]?\d+)?|[+\-]?\d+(?:\/[+\-]?\d+)?|#b[+\-]?[01]+|#o[+\-]?[0-7]+|#x[+\-]?[\da-f]+)/;
4 var symbol = /[^\s'`,@()\[\]";]/;
7 function readSym(stream) {
9 while (ch = stream.next()) {
10 if (ch == "\\") stream.next();
11 else if (!symbol.test(ch)) { stream.backUp(1); break; }
13 return stream.current();
16 function base(stream, state) {
17 if (stream.eatSpace()) {type = "ws"; return null;}
18 if (stream.match(numLiteral)) return "number";
19 var ch = stream.next();
20 if (ch == "\\") ch = stream.next();
22 if (ch == '"') return (state.tokenize = inString)(stream, state);
23 else if (ch == "(") { type = "open"; return "bracket"; }
24 else if (ch == ")" || ch == "]") { type = "close"; return "bracket"; }
25 else if (ch == ";") { stream.skipToEnd(); type = "ws"; return "comment"; }
26 else if (/['`,@]/.test(ch)) return null;
28 if (stream.skipTo("|")) { stream.next(); return "symbol"; }
29 else { stream.skipToEnd(); return "error"; }
30 } else if (ch == "#") {
31 var ch = stream.next();
32 if (ch == "[") { type = "open"; return "bracket"; }
33 else if (/[+\-=\.']/.test(ch)) return null;
34 else if (/\d/.test(ch) && stream.match(/^\d*#/)) return null;
35 else if (ch == "|") return (state.tokenize = inComment)(stream, state);
36 else if (ch == ":") { readSym(stream); return "meta"; }
39 var name = readSym(stream);
40 if (name == ".") return null;
42 if (name == "nil" || name == "t") return "atom";
43 if (name.charAt(0) == ":") return "keyword";
44 if (name.charAt(0) == "&") return "variable-2";
49 function inString(stream, state) {
50 var escaped = false, next;
51 while (next = stream.next()) {
52 if (next == '"' && !escaped) { state.tokenize = base; break; }
53 escaped = !escaped && next == "\\";
58 function inComment(stream, state) {
60 while (next = stream.next()) {
61 if (next == "#" && last == "|") { state.tokenize = base; break; }
69 startState: function () {
70 return {ctx: {prev: null, start: 0, indentTo: 0}, tokenize: base};
73 token: function (stream, state) {
74 if (stream.sol() && typeof state.ctx.indentTo != "number")
75 state.ctx.indentTo = state.ctx.start + 1;
78 var style = state.tokenize(stream, state);
80 if (state.ctx.indentTo == null) {
81 if (type == "symbol" && assumeBody.test(stream.current()))
82 state.ctx.indentTo = state.ctx.start + config.indentUnit;
84 state.ctx.indentTo = "next";
85 } else if (state.ctx.indentTo == "next") {
86 state.ctx.indentTo = stream.column();
89 if (type == "open") state.ctx = {prev: state.ctx, start: stream.column(), indentTo: null};
90 else if (type == "close") state.ctx = state.ctx.prev || state.ctx;
94 indent: function (state, _textAfter) {
95 var i = state.ctx.indentTo;
96 return typeof i == "number" ? i : state.ctx.start + 1;
100 blockCommentStart: "#|",
101 blockCommentEnd: "|#"
105 CodeMirror.defineMIME("text/x-common-lisp", "commonlisp");