在Emacs24 python模式下,如何自定义每种语法的颜色?

问题描述 投票:0回答:3

例如,我希望“ while”为蓝色,“ for”为绿色,该怎么做?除颜色之外,我可以将语法设为粗体还是斜体?提前非常感谢。

emacs python-mode
3个回答
1
投票

最简单的方法是将光标放入给定颜色的字符串中,然后键入M-xset-face-foregroundEnter。然后只需确认脸部名称并指定颜色即可。要将脸部设为粗体或斜体,请以类似方式使用set-face-font

您可以将设置保存到.emacs文件中:

(set-face-foreground 'font-lock-comment-face "gray")

1
投票

由于以下关键字的ALLpython.el中定义为python-font-lock-keywords,因此您需要使用一些不同的字体来代替其中一些,或者破解这些相同关键字的源以使其具有不同的字体:

“ and”“ del”“ from”“不是”“ while”“ as”“ elif”“ global”“ or”“ with”“ assert”“ else”“ if”“ pass”“ yield”“ break” “除”,“导入”,“类”,“中”,“筹集”,“继续”,“最终”,“是”,“返回”,“ def”,“ for”,“ lambda”,“ try”,“ print”,“ exec”,“ nonlocal”,“ self” “。

下面的代码是一个示例,该示例如何将在python-font-lock-keywords中已定义的某些关键字胜过python.el -在此示例中,while为蓝色,并带有bold;并且,forbolditalics呈绿色。 python-font-lock-keywords不会被特别定义的字体所占优势将默认为font-lock-keyword-face-我也包括了对该字体的示例修改:

(custom-set-faces
  '(font-lock-keyword-face
     ((t (:background "white" :foreground "red" :bold t))))
  )

(defvar lawlist-blue (make-face 'lawlist-blue))
(set-face-attribute 'lawlist-blue nil
  :background "white" :foreground "blue" :bold t)

(defvar lawlist-green (make-face 'lawlist-green))
(set-face-attribute 'lawlist-green nil
  :background "white" :foreground "green" :bold t :italic t)

(defvar lawlist-keywords-01
  (concat "\\b\\(?:"
    (regexp-opt (list "hello" "world" "while" ))
  "\\)\\b"))

(defvar lawlist-keywords-02
  (concat "\\b\\(?:"
    (regexp-opt (list "foo" "bar" "for" ))
  "\\)\\b"))

(font-lock-add-keywords 'python-mode (list

  (list (concat
    "\\("lawlist-keywords-01"\\)") 1 'lawlist-blue t)

  (list (concat
    "\\("lawlist-keywords-02"\\)") 1 'lawlist-green t)

   ))

0
投票

另一种方法是使更改永久生效

M-x customize-face RET font-lock TAB

这会为您提供一个列表,以选择您要更改的面孔。在打开的缓冲区中,它会显示当前变量,您可以输入[选择]链接,然后从emacs的颜色变量中进行选择。

© www.soinside.com 2019 - 2024. All rights reserved.