【问题标题】:How to customize emacs in python mode to highlight operators?python模式下如何自定义emacs高亮操作符?
【发布时间】:2026-01-09 04:25:06
【问题描述】:

我正在将 emacs 设置为我的 python IDE,并且我在网上找到了大量解释自动完成以及各种其他功能的材料。不过,我想不通的是如何让语法高亮器对运算符进行高亮显示。

如何在 python 模式下自定义我的 emacs 以制作 + - 不同的颜色?我还希望它使整数、浮点数和括号也有不同的颜色。

【问题讨论】:

    标签: emacs


    【解决方案1】:

    我的编程模式实际上有类似的设置。这定义了 2 个单独的面,一个用于运算符,一个用于“结束语句”符号(显然在 python 中不是那么有用)。您可以修改 "\\([][|!.+=&/%*,<>(){}:^~-]+\\)" 正则表达式以匹配您感兴趣的运算符并将面自定义为您想要的颜色。

    (defvar font-lock-operator-face 'font-lock-operator-face)
    
    (defface font-lock-operator-face
      '((((type tty) (class color)) nil)
        (((class color) (background light))
         (:foreground "dark red"))
        (t nil))
      "Used for operators."
      :group 'font-lock-faces)
    
    (defvar font-lock-end-statement-face 'font-lock-end-statement-face)
    
    (defface font-lock-end-statement-face
      '((((type tty) (class color)) nil)
        (((class color) (background light))
         (:foreground "DarkSlateBlue"))
        (t nil))
      "Used for end statement symbols."
      :group 'font-lock-faces)
    
    (defvar font-lock-operator-keywords
      '(("\\([][|!.+=&/%*,<>(){}:^~-]+\\)" 1 font-lock-operator-face)
        (";" 0 font-lock-end-statement-face)))
    

    然后,您只需通过向相关模式添加挂钩来启用此功能(此示例假设您使用的是“python-mode”):

    (add-hook 'python-mode-hook
                      '(lambda ()
                         (font-lock-add-keywords nil font-lock-operator-keywords t))
                      t t)
    

    【讨论】:

    • 我已将此处的文本复制并粘贴到我的 .emacs 文件中并在 python 模式下运行它,它不会更改语法突出显示。例如,键入分号不会导致突出显示。我没有足够的自定义 emacs 来解决它,但是......我做错了什么?
    • 当你的光标在一个操作符上时,输入“M-x describe-faces”。这是否列出了 font-lock-operator-face?
    • 哎呀,我的意思是“M-x describe-text-properties”。
    • 不。它只是说字体化的 t。
    • 如果你运行“M-x describe-variable font-lock-keywords”,你看到这个变量的值中列出的 font-lock-operator 东西了吗?而且,如果您运行“M-x describe-variable python-mode-hook”,您是否还会看到操作符的内容? (注意,我假设你有启用字体锁定模式)。
    【解决方案2】:

    将以下内容放入您的 ~/.emacs 文件中:

    ;
    ; Python operator and integer highlighting
    ; Integers are given font lock's "constant" face since that's unused in python
    ; Operators, brackets are given "widget-inactive-face" for convenience to end-user 
    ;
    
    (font-lock-add-keywords 'python-mode
        '(("\\<\\(object\\|str\\|else\\|except\\|finally\\|try\\|\\)\\>" 0 py-builtins-face)  ; adds object and str and fixes it so that keywords that often appear with : are assigned as builtin-face
        ("\\<[\\+-]?[0-9]+\\(.[0-9]+\\)?\\>" 0 'font-lock-constant-face) ; FIXME: negative or positive prefixes do not highlight to this regexp but does to one below
        ("\\([][{}()~^<>:=,.\\+*/%-]\\)" 0 'widget-inactive-face)))
    

    【讨论】:

      【解决方案3】:

      添加我自己的答案,因为我无法让其他人工作(截至 emacs 25.3.1, 2017)。

      以下省略号可用于突出显示运算符:

      ;; Operator Fonts
      (defface font-lock-operator-face
       '((t (:foreground "#8b8bcd"))) "Basic face for operator." :group 'basic-faces)
      
      ;; C-Like
      (dolist (mode-iter '(c-mode c++-mode glsl-mode java-mode javascript-mode rust-mode))
        (font-lock-add-keywords mode-iter
         '(("\\([~^&\|!<>=,.\\+*/%-]\\)" 0 'font-lock-operator-face keep))))
      ;; Scripting
      (dolist (mode-iter '(python-mode lua-mode))
        (font-lock-add-keywords mode-iter
         '(("\\([@~^&\|!<>:=,.\\+*/%-]\\)" 0 'font-lock-operator-face keep))))
      

      【讨论】:

        最近更新 更多