Ask HN: How do I customize code coloring in emacs?

11 points by andrewljohnson ↗ HN
I program Django/Python in emacs, and I would like things like {% comment %} FOO {% endcomment %} to turn orange.

How can I set up some colors for important Django template tags?

If you can give me a bit of example code, that would be great. A link to an example of this would be great too.

8 comments

[ 2.5 ms ] story [ 30.5 ms ] thread
http://lmgtfy.com/?q=django+emacs

To change the color of a specific face in emacs, go to the face and do M-x set-face-foreground Return and put the color you want.

downvoted because he probably expected suggestions about which mode is best, etc. rather than snark
Download color-theme. http://www.emacswiki.org/cgi-bin/wiki?ColorTheme

Choose one of the included themes that you like the most, and start customizing.

Personally, I like zenburn. http://www.brockman.se/software/zenburn/zenburn.el But that might be a bit heavy to start off with.

Or, the shortcut way, define some stuff in custom-set-faces. e.g.:

    (custom-set-faces
      ;; custom-set-faces was added by Custom.
      ;; If you edit it by hand, you could mess it up, so be careful.
      ;; Your init file should contain only one such instance.
      ;; If there is more than one, they won't work right.
     '(paren-face-match-light ((t (:background "#5f5f5f" :foreground "#e37170" :weight ultra-bold))))
     '(paren-face-match ((t (:background "#5f5f5f" :foreground "#e37170" :weight ultra-bold))))
     '(paren-face-mismatch ((((class color)) (:background "#332323" :foreground "#e37170"))))
     '(paren-face-no-match ((((class color)) (:background "#332323" :foreground "#e37170"))))
     '(linum ((t (:foreground "#5f5f5f")))))
Of course, this all requires that your Django/Python mode has a face for the {% ... %} stuff. I've never done any Django work, so I'm not sure if it does. If it doesn't, you'll have to hack at the Django/Python mode some to define that face so you can color it.
(color-theme-midnight)

is my preference

Thanks for the note. I use nice faces for my JS, Python, HTML, and CSS. I just wasn't sure how to go about customizing.

I got this snippet, and I think I can take it from here:

  (defun django-highlight-comments ()
    (interactive "p")
    (highlight-regexp "{%.*?%}" 'hi-orange))
  (add-hook 'html-mode-hook 'django-highlight-comments)
instead of using a hack like highlight-regexp, perhaps it'd be better to place a bug or hack the mode itself :)