Skip to content

[Fix #277] Apply font-lock-comment-face to (comment) and #_ #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### New features

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

### Bugs fixed

Expand Down
44 changes: 44 additions & 0 deletions clojure-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,49 @@ If JUSTIFY is non-nil, justify as well as fill the paragraph."
(do-auto-fill)))))


;;; #_ comments font-locking
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment disagrees a bit with actual code (as it affects comment as well).

;; Code heavily borrowed from Slime.
;; https://github.com/slime/slime/blob/master/contrib/slime-fontifying-fu.el#L186
(defvar clojure--comment-macro-regexp
(rx "#_" (* " ") (group-n 1 (not (any " "))))
"Regexp matching the start of a comment sexp.
The beginning of match-group 1 should be before the sexp to be
marked as a comment. The end of sexp is found with
`clojure-forward-logical-sexp'.

By default, this only applies to code after the `#_' reader
macro. In order to also font-lock the `(comment ...)' macro as a
comment, you can set the value to:
\"#_ *\\\\(?1:[^ ]\\\\)\\\\|\\\\(?1:(comment\\\\_>\\\\)\"")

(defun clojure--search-comment-macro-internal (limit)
(when (search-forward-regexp clojure--comment-macro-regexp limit t)
(let* ((md (match-data))
(start (match-beginning 1))
(state (syntax-ppss start)))
;; inside string or comment?
(if (or (nth 3 state)
(nth 4 state))
(clojure--search-comment-macro-internal limit)
(goto-char start)
(clojure-forward-logical-sexp 1)
;; Data for (match-end 1).
(setf (elt md 3) (point))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've almost never seen setf in Emacs Lisp code (although it was quite popular in CL).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost always use it to destructively change a sequence or struct. :-)
It's considerably more convenient than something like (setcar (cdr (cdr (cdr md))) start).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. :-) I just said it's pretty uncommon.

(set-match-data md)
t))))

(defun clojure--search-comment-macro (limit)
"Find comment macros and set the match data."
(let ((result 'retry))
(while (and (eq result 'retry) (<= (point) limit))
(condition-case nil
(setq result (clojure--search-comment-macro-internal limit))
(end-of-file (setq result nil))
(scan-error (setq result 'retry))))
result))


;;; General font-locking
(defun clojure-match-next-def ()
"Scans the buffer backwards for the next \"top-level\" definition.
Called by `imenu--generic-function'."
Expand Down Expand Up @@ -499,6 +541,8 @@ any number of matches of `clojure--sym-forbidden-rest-chars'."))
(1 font-lock-type-face nil t))
;; fooBar
("\\(?:\\<\\|/\\)\\([a-z]+[A-Z]+[a-zA-Z0-9$]*\\>\\)" 1 'clojure-interop-method-face)
;; #_ and (comment ...) macros.
(clojure--search-comment-macro 1 font-lock-comment-face t)
;; Highlight `code` marks, just like `elisp'.
(,(rx "`" (group-n 1 (optional "#'")
(+ (or (syntax symbol) (syntax word)))) "`")
Expand Down
5 changes: 5 additions & 0 deletions test/clojure-mode-font-lock-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ POS."
(should (equal (clojure-test-face-at 2 12 "(while-alist [x 1]\n ())") 'font-lock-keyword-face))
(should (equal (clojure-test-face-at 2 10 "(let-alist [x 1]\n ())") 'font-lock-keyword-face)))

(ert-deftest clojure-mode-syntax-table/comment-macros ()
:tags '(fontification syntax-table)
(should (not (clojure-test-face-at 1 2 "#_ \n;; some crap\n (lala 0101\n lao\n\n 0 0i)")))
(should (equal (clojure-test-face-at 5 41 "#_ \n;; some crap\n (lala 0101\n lao\n\n 0 0i)") 'font-lock-comment-face)))

(ert-deftest clojure-mode-syntax-table/type ()
:tags '(fontification syntax-table)
(should (eq (clojure-test-face-at 1 9 "SomeClass") 'font-lock-type-face)))
Expand Down