如何在python模式下自定义emacs以突出显示运算符?

发布于 2024-11-04 18:09:23 字数 167 浏览 0 评论 0原文

我正在将 emacs 设置为我的 python IDE,并且我在网上找到了大量解释自动完成以及各种其他功能的材料。但我不明白的是如何让语法突出显示在运算符上发挥突出显示的魔力。

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

I'm setting up emacs to be my python IDE, and I've found plenty of material online that explain auto-completion, among a variety of other features. What I can't figure out, though, is how to get the syntax highlighter to do its highlighting magic on operators.

How can I customize my emacs in python mode to make + - different colors? I'd also like it to make integers, floats, and parentheses different colors as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满身野味 2024-11-11 18:09:23

我的编程模式实际上有类似的设置。这定义了 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)

i actually have something like this setup for my programming modes. this defines 2 separate faces, one for operators and one for the "end statement" symbol (not so useful in python, obviously). you can modify the "\\([][|!.+=&/%*,<>(){}:^~-]+\\)" regex to match the operators you are interested in and customize the faces to be the color you want.

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

then, you just enable this by adding a hook to the relevant modes (this example assumes you are using "python-mode"):

(add-hook 'python-mode-hook
                  '(lambda ()
                     (font-lock-add-keywords nil font-lock-operator-keywords t))
                  t t)
仅一夜美梦 2024-11-11 18:09:23

将以下内容放入 ~/.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)))

Put the following in your ~/.emacs file:

;
; 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)))
时间海 2024-11-11 18:09:23

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

以下 elisp 可用于突出显示运算符:

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

Adding my own answer since I couldn't get the others working (as of emacs 25.3.1, 2017).

The following elisp can be used to highlight operators:

;; 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))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文