Emacs font locking: highlighting comment lines

posted in Programmology
Published June 10, 2009
Advertisement
Posted from http://jlongster.com/blog/2009/06/9/font-locking-highlighting-code-section-headers/.

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

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 &#106avascript). 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 "/"))))
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement