• entries
73
131
• views
54782

# Emacs font locking: highlighting comment lines

233 views

----------------------------------------

Emacs font locking: highlighting comment lines

June 09, 2009

I recently tried an package called Quack which adds a bunch of commands for writing and running Scheme code in Emacs. Although I don't use it anymore, one thing I really like was how it highlighted comment lines with a depth of 3 or more (meaning ";;;" in Scheme, or "///" in C or javascript). It turns out this was really nice, since it ends up highlighting file-level comments at the top and section headers throughout the code.

I think this looks much nicer than other techniques, such as repeating the comment character 40 times to indicate a line (e.g. ";;;;;;;;;;;;" over and over). If everyone on a project wrote code this simple way, each person would get to choose how to view those kinds of comments.

I wanted to separate this functionality into its own package so that I could use it in languages other than Scheme. I have never written any Emacs Lisp before, so although it was pretty brutal, I ended up with something that works rather nicely. Use it if you want.

(defconst jwl-title-face 'jwl-title-face)(defface jwl-title-face  '((((class color) (background light))     (:background "DarkSeaGreen1" :foreground "grey25"))    (((class color) (background dark))     (:background "DarkGrey")))  "Face used for titles.")(defun jwl-generate-highlight-keywords (comment-char)  ((,(concat "^\$$[ \t]*" comment-char comment-char comment-char "[ \t]*" "\\(" "[^\r\n]*" "\$$"              "\r?\n?\\)")     1 jwl-title-face prepend)))(add-hook 'scheme-mode-hook (lambda ()   (font-lock-add-keywords    nil    (jwl-generate-highlight-keywords ";"))))(add-hook 'python-mode-hook (lambda ()   (font-lock-add-keywords    nil    (jwl-generate-highlight-keywords "#"))))(add-hook 'js2-mode-hook (lambda ()   (font-lock-add-keywords    nil    (jwl-generate-highlight-keywords "/"))))`

There are no comments to display.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account