在Emacs24 python模式下,如何自定义每个语法';什么颜色?

在Emacs24 python模式下,如何自定义每个语法';什么颜色?,emacs,python-mode,Emacs,Python Mode,例如,我希望“while”是蓝色的,“For”是绿色的,怎么做?除了颜色之外,我可以将语法设置为粗体还是斜体?非常感谢您。最简单的方法是将光标放入给定颜色的字符串中,然后键入M-xset face frontEnter。然后确认面名称并指定颜色。要将面部设置为粗体或斜体,请以类似方式使用set face font 您可以将设置保存到.emacs文件中: (set-face-foreground 'font-lock-comment-face "gray") 如果启用了hl线路模式,这在最新的E

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

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

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

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

如果启用了
hl线路模式
,这在最新的Emacs版本中不起作用。您需要从C-uC-x=中获取面名称,因为
设置面前景
不会自动完成面名称,而是
hl行
面。

最简单的方法是将光标放入给定颜色的字符串中,然后键入M-x
设置面前景
回车。然后确认面名称并指定颜色。要将面部设置为粗体或斜体,请以类似方式使用
set face font

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

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

如果启用了
hl线路模式
,这在最新的Emacs版本中不起作用。您需要从C-uC-x=中获取人脸名称,因为
设置人脸前景
不会自动完成人脸名称,而是
hl行
人脸。

因为
以下所有关键字都在
python.el
中定义为
python字体锁定关键字
,您需要使用不同的字体来胜过其中一些关键字,或者对这些相同关键字的来源进行黑客攻击,使其具有不同的字体:

和“del”从“not”改为“del”,而“as”“elif”“global”或“assert”“else”,如果“raise”“continue”“finally”中的“pass”“yield”“break”“import”“class”“是”“return”“def”“for”“lambda”“try”“print”“exec”“nonlocal”“self”

下面的代码是一个示例,说明了如何在
python.el
中定义的一些关键字中胜过
python字体锁定关键字
——在本例中,
是蓝色的,带有粗体;而且,
for
是绿色的,带有粗体和斜体。
python字体锁定关键字
如果没有特别定义的字体面,则默认为
font-lock关键字面
——我还提供了对该面的示例修改:

(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)

   ))

由于
以下所有
关键字都在
python.el
中定义为
python字体锁定关键字
,因此您需要使用不同的字体来取代其中的一些关键字,或者对这些相同关键字的源代码进行修改,使其具有不同的字体:

和“del”从“not”改为“del”,而“as”“elif”“global”或“assert”“else”,如果“raise”“continue”“finally”中的“pass”“yield”“break”“import”“class”“是”“return”“def”“for”“lambda”“try”“print”“exec”“nonlocal”“self”

下面的代码是一个示例,说明了如何在
python.el
中定义的一些关键字中胜过
python字体锁定关键字
——在本例中,
是蓝色的,带有粗体;而且,
for
是绿色的,带有粗体和斜体。
python字体锁定关键字
如果没有特别定义的字体面,则默认为
font-lock关键字面
——我还提供了对该面的示例修改:

(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)

   ))

另一种方法是使改变永久化

M-x customize-face RET font-lock TAB
这将为您提供一个列表,供您选择要更改的面。
从打开的缓冲区显示当前变量,您可以输入[Choose]链接并从emacs的颜色变量中选择。另一种方法是使更改永久化

M-x customize-face RET font-lock TAB
这将为您提供一个列表,供您选择要更改的面。
从打开的缓冲区显示当前变量,您可以输入[Choose]链接并从emacs的颜色变量中选择。

代码进入
.emacs
文件,然后重新启动应用程序。然后,在python模式下,使用
while
进行实验,使用
进行实验--
你会看到颜色和字体是你在上面的第一个问题中要求的。是否可以在
python字体锁定关键字列表中添加一些关键字?@alper--我在这个帖子中回答这个问题已经快8年了,随着时间的推移,我可能学会了一些新的技巧。在此期间,Emacs也进行了修订。如果您在emacs.stackexchange.com上打开一个新问题,如果这里或那里都没有答案,可能会更好……请参见://需要花费大量精力才能坚持某个问题这么长时间,这是令人敬畏的代码进入
.emacs
文件,然后重新启动应用程序。然后,在python模式下,使用
while
进行实验,使用
进行实验--
你会看到颜色和字体是你在上面的第一个问题中要求的。是否可以在
python字体锁定关键字列表中添加一些关键字?@alper--我在这个帖子中回答这个问题已经快8年了,随着时间的推移,我可能学会了一些新的技巧。在此期间,Emacs也进行了修订。如果你在emacs.stackexchange.com上打开一个新问题,如果这里或那里都没有答案,那可能会更好……请参见://要坚持这么长时间,需要付出很多努力,这太棒了