【发布时间】:2009-05-17 21:26:58
【问题描述】:
我在 emacs 中编写 Django/Python,我希望像 {% comment %} FOO {% endcomment %} 这样的东西变成橙色。
如何为重要的 Django 模板标签设置一些颜色?
【问题讨论】:
标签: python django emacs syntax-highlighting
我在 emacs 中编写 Django/Python,我希望像 {% comment %} FOO {% endcomment %} 这样的东西变成橙色。
如何为重要的 Django 模板标签设置一些颜色?
【问题讨论】:
标签: python django emacs syntax-highlighting
您可以使用django-mode 或MuMaMo 等专用模式。
如果您想要一些非常基本的东西,并且假设您在 html-mode 中进行编辑,您可以尝试以下操作:
(defun django-highlight-comments ()
(interactive "p")
(highlight-regexp "{%.*?%}" 'hi-orange))
(add-hook 'html-mode-hook 'django-highlight-comments)
(只需将以上行添加到您的.emacs 或init.el,然后对其进行评估或重新启动emacs)。
【讨论】:
这就是我所做的。它比上面的代码更通用一点,它使用了内置的字体锁定机制。
(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)))
【讨论】: