Skip to content

Commit 5f591a0

Browse files
nbeloglazovmarijnh
authored andcommitted
Add character higlighting to clojure mode.
1 parent 965dc47 commit 5f591a0

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

mode/clojure/clojure.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Branched from CodeMirror's Scheme mode (by Koh Zi Han, based on implementation by Koh Zi Chun)
44
*/
55
CodeMirror.defineMode("clojure", function () {
6-
var BUILTIN = "builtin", COMMENT = "comment", STRING = "string",
6+
var BUILTIN = "builtin", COMMENT = "comment", STRING = "string", CHARACTER = "string-2",
77
ATOM = "atom", NUMBER = "number", BRACKET = "bracket", KEYWORD = "keyword";
88
var INDENT_WORD_SKIP = 2;
99

@@ -95,6 +95,20 @@ CodeMirror.defineMode("clojure", function () {
9595
return false;
9696
}
9797

98+
// Eat character that starts after backslash \
99+
function eatCharacter(stream) {
100+
var first = stream.next();
101+
// Read special literals: backspace, newline, space, return.
102+
// Just read all lowercase letters.
103+
if (first.match(/[a-z]/) && stream.match(/[a-z]+/, true)) {
104+
return;
105+
}
106+
// Read unicode character: \u1000 \uA0a1
107+
if (first === "u") {
108+
stream.match(/[0-9a-z]{4}/i, true);
109+
}
110+
}
111+
98112
return {
99113
startState: function () {
100114
return {
@@ -135,6 +149,9 @@ CodeMirror.defineMode("clojure", function () {
135149
if (ch == "\"") {
136150
state.mode = "string";
137151
returnType = STRING;
152+
} else if (ch == "\\") {
153+
eatCharacter(stream);
154+
returnType = CHARACTER;
138155
} else if (ch == "'" && !( tests.digit_or_colon.test(stream.peek()) )) {
139156
returnType = ATOM;
140157
} else if (ch == ";") { // comment

mode/clojure/index.html

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ <h1>CodeMirror: Clojure mode</h1>
2121

2222
;; Core game of life's algorithm functions
2323

24-
(defn neighbours
24+
(defn neighbours
2525
"Given a cell's coordinates, returns the coordinates of its neighbours."
2626
[[x y]]
2727
(for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])]
2828
[(+ dx x) (+ dy y)]))
2929

30-
(defn step
30+
(defn step
3131
"Given a set of living cells, computes the new set of living cells."
3232
[cells]
3333
(set (for [[cell n] (frequencies (mapcat neighbours cells))
@@ -36,14 +36,14 @@ <h1>CodeMirror: Clojure mode</h1>
3636

3737
;; Utility methods for displaying game on a text terminal
3838

39-
(defn print-board
39+
(defn print-board
4040
"Prints a board on *out*, representing a step in the game."
4141
[board w h]
4242
(doseq [x (range (inc w)) y (range (inc h))]
43-
(if (= y 0) (print "\n"))
43+
(if (= y 0) (print "\n"))
4444
(print (if (board [x y]) "[X]" " . "))))
4545

46-
(defn display-grids
46+
(defn display-grids
4747
"Prints a squence of boards on *out*, representing several steps."
4848
[grids w h]
4949
(doseq [board grids]
@@ -52,11 +52,20 @@ <h1>CodeMirror: Clojure mode</h1>
5252

5353
;; Launches an example board
5454

55-
(def
55+
(def
5656
^{:doc "board represents the initial set of living cells"}
5757
board #{[2 1] [2 2] [2 3]})
5858

59-
(display-grids (take 3 (iterate step board)) 5 5) </textarea></form>
59+
(display-grids (take 3 (iterate step board)) 5 5)
60+
61+
;; Let's play with characters
62+
(println \1 \a \# \\
63+
\" \( \newline
64+
\} \" \space
65+
\tab \return \backspace
66+
\u1000 \uAaAa \u9F9F)
67+
68+
</textarea></form>
6069
<script>
6170
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
6271
</script>

0 commit comments

Comments
 (0)