Better Comments or tooling as a time sink

The other day my friend and fellow CS Ed Blogger Alfred Thompson wrote about Better Comments, an extension for visual studio that displays comments that are marked up with special characters in order to highlight them. Here's a screenshot:

ClassificationC.png

So, the first thing I though was "I bet emacs could do that pretty easily" and down the rabbit hole I went.

I had to figure out something about how emacs themes and font-locking (emacs for syntax highlighting) works and of course spent far too much time learning about Emacs, my favored tool, rather than getting actual work done, but I came up with this:

(make-face 'font-lock-comment-important)
(set-face-foreground 'font-lock-comment-important "#00ff00")

(make-face 'font-lock-comment-todo)
(set-face-foreground 'font-lock-comment-todo "#ff0000")

(make-face 'font-lock-comment-strike)
(set-face-attribute 'font-lock-comment-strike
		    nil :strike-through t)

(defun add-custom-keyw()
  "adds a few special keywords"
  (font-lock-add-keywords 
   nil
   '(("cx \\(.+\\)" 1 'font-lock-comment-strike prepend)
     ("ct \\(.+\\)" 1 'font-lock-comment-todo prepend)
     ("ci \\(.+\\)" 1 'font-lock-comment-important prepend)
     )
   ))
(add-hook 'python-mode-hook 'add-custom-keyw)
(add-hook 'js2-mode-hook 'add-custom-keyw)
(add-hook 'js-mode-hook 'add-custom-keyw)

It's a hack but it does indeed work. In action, it looks something like this:

Truth be told, it really colors anything following cx, ct, or ci, not just in comments – I have to look a bit more into how emacs handles comments to figure that one out.

In any event, even though I spent too much time doing this, it's nice to know I'm working in a tool in which I can.

UPDATE: Thanks to user ncsuwolf on /r/emacs on reddit, here's a fully working, more properly done solution:

(defface font-lock-comment-strike
  '((t (:strike-through t)))
  "For strike-through comments")

(defface font-lock-comment-important
  '((t (:foreground "#00ff00")))
  "For important")
(defface font-lock-comment-todo
  '((t (:foreground "#ff0000"))
		"For todo comments")
(defun add-custom-keyw()
		"adds a few special keywords"
		(font-lock-add-keywords
		 nil
		 '(("\\s<+x[[:space:]]*\\(.*?\\)[[:space:]]*\\s>" 1 'font-lock-comment-strike prepend)
			 ("\\s<+t[[:space:]]*\\(.*?\\)[[:space:]]*\\s>" 1 'font-lock-comment-todo prepend)
			 ("\\s<+i[[:space:]]*\\(.*?\\)[[:space:]]*\\s>" 1 'font-lock-comment-important prepend))))

	(add-hook 'prog-mode-hook #'add-custom-keyw)

Comments

Comments powered by Disqus



Enter your email address:

Delivered by FeedBurner

Google Analytics Alternative