Skip to content

Commit e440080

Browse files
rvlobbatsov
authored andcommitted
Fix for character literal font-lock (#588)
When inside a vector or before white space, some escaped characters, like comma, square and curly brackets, weren't being formatted. * Added tests. * Added examples in test.clj for visual inspection.
1 parent 8280e44 commit e440080

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* [#574](https://github.com/clojure-emacs/clojure-mode/issues/574): Remove `clojure-view-grimoire` command.
1616
* Stop `clojure-sort-ns` from calling `redisplay`.
1717
* [#584](https://github.com/clojure-emacs/clojure-mode/issues/584): Align to recent `pcase` changes on Emacs master.
18+
* [#588](https://github.com/clojure-emacs/clojure-mode/pull/588): Fix font-lock for character literals.
1819

1920
## 5.12.0 (2020-08-13)
2021

clojure-mode.el

+10-10
Original file line numberDiff line numberDiff line change
@@ -801,8 +801,6 @@ any number of matches of `clojure--sym-forbidden-rest-chars'."))
801801
"\\(\\sw+\\)?" )
802802
(1 font-lock-keyword-face)
803803
(2 font-lock-function-name-face nil t))
804-
;; lambda arguments - %, %&, %1, %2, etc
805-
("\\<%[&1-9]?" (0 font-lock-variable-name-face))
806804
;; Special forms
807805
(,(concat
808806
"("
@@ -862,14 +860,16 @@ any number of matches of `clojure--sym-forbidden-rest-chars'."))
862860
"\\>")
863861
0 font-lock-constant-face)
864862
;; Character literals - \1, \a, \newline, \u0000
865-
(,(rx "\\" (or any
866-
"newline" "space" "tab" "formfeed" "backspace"
867-
"return"
868-
(: "u" (= 4 (char "0-9a-fA-F")))
869-
(: "o" (repeat 1 3 (char "0-7"))))
870-
word-boundary)
871-
0 'clojure-character-face)
872-
863+
(,(rx (group "\\" (or any
864+
"newline" "space" "tab" "formfeed" "backspace"
865+
"return"
866+
(: "u" (= 4 (char "0-9a-fA-F")))
867+
(: "o" (repeat 1 3 (char "0-7")))))
868+
(or (not word) word-boundary))
869+
1 'clojure-character-face)
870+
;; lambda arguments - %, %&, %1, %2, etc
871+
;; must come after character literals for \% to be handled properly
872+
("\\<%[&1-9]?" (0 font-lock-variable-name-face))
873873
;; namespace definitions: (ns foo.bar)
874874
(,(concat "(\\<ns\\>[ \r\n\t]*"
875875
;; Possibly metadata, shorthand and/or longhand

test.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
minnow))
5252

5353
;; character literals
54-
[\a \newline \u0032 \/ \+ \,, \;]
54+
[\a \newline \u0032 \/ \+ \,, \; \( \% \)]
5555

5656
;; TODO change font-face for sexps starting with @,#
5757
(comment ;; examples

test/clojure-mode-font-lock-test.el

+16
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,22 @@ DESCRIPTION is the description of the spec."
942942
("\\"
943943
(1 2 clojure-character-face)))
944944

945+
(when-fontifying-it "should handle characters not by themselves"
946+
("[\\,,]"
947+
(1 1 nil)
948+
(2 3 clojure-character-face)
949+
(4 5 nil))
950+
951+
("[\\[]"
952+
(1 1 nil)
953+
(2 3 clojure-character-face)
954+
(4 4 nil)))
955+
956+
(when-fontifying-it "should handle % character literal"
957+
("#(str \\% %)"
958+
(7 8 clojure-character-face)
959+
(10 10 font-lock-variable-name-face)))
960+
945961
(when-fontifying-it "should handle referred vars"
946962
("foo/var"
947963
(1 3 font-lock-type-face))

0 commit comments

Comments
 (0)