Emacs custom background color by mode

≯℡__Kan透↙ 提交于 2020-01-29 13:11:11

问题


I use emacs to edit a number of file types, and would like an easy visual queue to tell .c files from .vhd or .py files, for instance. How can I add a custom background color to the major mode for that language?


回答1:


You can do this via hooks. Among other things you can hook is when a new major mode starts. Put something like this into your .emacs file, and emacs will set your background color to purple every time you go into Python mode.

  (add-hook 'python-mode-hook
            (lambda ()
              (set-background-color "purple")))

Resetting the background color to the default in the case that you switch back to a mode that doesn't have an explicit set-background hook for it is left as an exercise for the reader.

You can also manually set the background color with M-x set-background-color




回答2:


You cannot set the background color on a buffer-by-buffer basis. See the SU question How can I change the background colour of a single emacs buffer?.

The first answer there shows how you can change the background for a single Emacs frame, which might work for you if you have one frame per file (or per mode).




回答3:


For posterity, as this thread is 4 years old, it is now possible in Emacs 24.4+ to change faces on a buffer local level. Simply define a face and use (face-remap-add-relative) to swap out whatever face you want with it.

Define a defface:

(defface my-special-face '((t :background "aqua")))

Then add a hook to the mode of your choice:

(add-hook 'python-mode-hook
          (lambda ()
            (face-remap-add-relative 'default 'my-special-face)))


来源:https://stackoverflow.com/questions/3094638/emacs-custom-background-color-by-mode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!