How can I color certain things in Emacs?

2019-05-05 18:29发布

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?

3条回答
疯言疯语
2楼-- · 2019-05-05 18:38

Here's what I do. It's a little more general than the code above, and it uses the built-in font-lock mechanisms.

(defvar django-tag-face (make-face 'django-tag-face))
(defvar django-variable-face (make-face 'django-variable-face))
(set-face-background 'django-tag-face "Aquamarine")
(set-face-foreground 'django-tag-face "Black")
(set-face-background 'django-variable-face "Plum")
(set-face-foreground 'django-variable-face "Black")


(font-lock-add-keywords
 'html-mode
 '(("\\({%[^%]*%}\\)" 1 django-tag-face prepend)
   ("\\({{[^}]*}}\\)" 1 django-variable-face prepend)))
查看更多
ゆ 、 Hurt°
3楼-- · 2019-05-05 18:44

Here are some links. I found them on the Google. It seems there is no one fully-complete and "official" solution to this problem, but a number of possibly quite usable substitutes avaliable.

查看更多
太酷不给撩
4楼-- · 2019-05-05 18:46

You could use dedicated modes like django-mode or MuMaMo.

If you want something very basic, and assuming you're editing in html-mode, you could try the following:

(defun django-highlight-comments ()
  (interactive "p")
  (highlight-regexp "{%.*?%}" 'hi-orange))
(add-hook 'html-mode-hook 'django-highlight-comments)

(Just add the above lines to your .emacs or init.el, and eval it or restart emacs).

查看更多
登录 后发表回答