Skip to content

Commit 9144360

Browse files
committed
Merge pull request #354 from clojure-emacs/comment-face
[Fix #277] Apply font-lock-comment-face to (comment) and #_
2 parents 6a01d68 + 51c8589 commit 9144360

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### New features
66

77
* Indent and font-lock forms that start with `let-`, `while-` or `when-` like their counterparts.
8+
* Apply the `font-lock-comment-face` to code commented out with `#_`.
89

910
### Bugs fixed
1011

clojure-mode.el

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,49 @@ If JUSTIFY is non-nil, justify as well as fill the paragraph."
327327
(do-auto-fill)))))
328328

329329

330+
;;; #_ comments font-locking
331+
;; Code heavily borrowed from Slime.
332+
;; https://github.com/slime/slime/blob/master/contrib/slime-fontifying-fu.el#L186
333+
(defvar clojure--comment-macro-regexp
334+
(rx "#_" (* " ") (group-n 1 (not (any " "))))
335+
"Regexp matching the start of a comment sexp.
336+
The beginning of match-group 1 should be before the sexp to be
337+
marked as a comment. The end of sexp is found with
338+
`clojure-forward-logical-sexp'.
339+
340+
By default, this only applies to code after the `#_' reader
341+
macro. In order to also font-lock the `(comment ...)' macro as a
342+
comment, you can set the value to:
343+
\"#_ *\\\\(?1:[^ ]\\\\)\\\\|\\\\(?1:(comment\\\\_>\\\\)\"")
344+
345+
(defun clojure--search-comment-macro-internal (limit)
346+
(when (search-forward-regexp clojure--comment-macro-regexp limit t)
347+
(let* ((md (match-data))
348+
(start (match-beginning 1))
349+
(state (syntax-ppss start)))
350+
;; inside string or comment?
351+
(if (or (nth 3 state)
352+
(nth 4 state))
353+
(clojure--search-comment-macro-internal limit)
354+
(goto-char start)
355+
(clojure-forward-logical-sexp 1)
356+
;; Data for (match-end 1).
357+
(setf (elt md 3) (point))
358+
(set-match-data md)
359+
t))))
360+
361+
(defun clojure--search-comment-macro (limit)
362+
"Find comment macros and set the match data."
363+
(let ((result 'retry))
364+
(while (and (eq result 'retry) (<= (point) limit))
365+
(condition-case nil
366+
(setq result (clojure--search-comment-macro-internal limit))
367+
(end-of-file (setq result nil))
368+
(scan-error (setq result 'retry))))
369+
result))
330370

371+
372+
;;; General font-locking
331373
(defun clojure-match-next-def ()
332374
"Scans the buffer backwards for the next \"top-level\" definition.
333375
Called by `imenu--generic-function'."
@@ -499,6 +541,8 @@ any number of matches of `clojure--sym-forbidden-rest-chars'."))
499541
(1 font-lock-type-face nil t))
500542
;; fooBar
501543
("\\(?:\\<\\|/\\)\\([a-z]+[A-Z]+[a-zA-Z0-9$]*\\>\\)" 1 'clojure-interop-method-face)
544+
;; #_ and (comment ...) macros.
545+
(clojure--search-comment-macro 1 font-lock-comment-face t)
502546
;; Highlight `code` marks, just like `elisp'.
503547
(,(rx "`" (group-n 1 (optional "#'")
504548
(+ (or (syntax symbol) (syntax word)))) "`")

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ POS."
130130
(should (equal (clojure-test-face-at 2 12 "(while-alist [x 1]\n ())") 'font-lock-keyword-face))
131131
(should (equal (clojure-test-face-at 2 10 "(let-alist [x 1]\n ())") 'font-lock-keyword-face)))
132132

133+
(ert-deftest clojure-mode-syntax-table/comment-macros ()
134+
:tags '(fontification syntax-table)
135+
(should (not (clojure-test-face-at 1 2 "#_ \n;; some crap\n (lala 0101\n lao\n\n 0 0i)")))
136+
(should (equal (clojure-test-face-at 5 41 "#_ \n;; some crap\n (lala 0101\n lao\n\n 0 0i)") 'font-lock-comment-face)))
137+
133138
(ert-deftest clojure-mode-syntax-table/type ()
134139
:tags '(fontification syntax-table)
135140
(should (eq (clojure-test-face-at 1 9 "SomeClass") 'font-lock-type-face)))

0 commit comments

Comments
 (0)