0

In Python, keyword arguments passed to functions can take the form of "keyword"="value", such as in this example:

some_function(somearg="foo", someotherarg=True)

I'm using python-mode in Emacs 27, and I'd like to change the color of the equals sign and/or the keyword, to make them more visually obvious (to me). How can I do this?

1 Answer 1

2

My proposal simply detects equal signs and assumes that the words placed before this sign are keywords.

(defface argument-face
  '((t (:foreground "orange")))
  "Face for argument"
  :group 'font-lock-faces)
(defface equal-face
  '((t (:foreground "red")))
  "Face for equal sign"
  :group 'font-lock-faces)
(font-lock-add-keywords 'python-mode
 `(("\\([[:word:]]*[_[[:word:]]*]*\\)\\(=\\)[^ ]"  (1 'argument-face)(2 'equal-face))))

The code has to be put in the initialization file.

2
  • Unfortunately, this does not distinguish between keyword asignments in function signatures, and assignments elsewhere in one's code.
    – mhucka
    Commented Jul 12, 2022 at 19:17
  • You are right. Note that I changed the code so that you get an invalid pattern if you add a space before or after the equal sign. This will allow you to mark the difference. Hopefully someone will provide you with a more elegant solution. Commented Jul 13, 2022 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.