Skip to content

Comment on Python Mode in Emacs

Comments

If you highlight the following code with python.el:

  a = 1
  b = 2
a and b will be highlighted in orange. However, in this code they will not:
  a, b = 1, 2
I tried to modify python.el so that variables defined by parallel assignment would get highlighted. I made the following imperfect change to python.el:
  <     ;; If parallel assignment is used, up to four variable names are highlighted.
  <     (,(rx line-start
  <           (zero-or-one (group (1+ (or word ?_))) (0+ space) "," (0+ space))
  <           (zero-or-one (group (1+ (or word ?_))) (0+ space) "," (0+ space))
  <           (zero-or-one (group (1+ (or word ?_))) (0+ space) "," (0+ space))
  <           (group (1+ (or word ?_))) (0+ space) "=")
  <      (1 font-lock-variable-name-face) (2 font-lock-variable-name-face)
  <      (3 font-lock-variable-name-face) (4 font-lock-variable-name-face))
  ---
  >     (,(rx line-start (group (1+ (or word ?_))) (0+ space) "=")
  >      (1 font-lock-variable-name-face))
The change is imperfect because it only highlights up to 4 variables created by parallel assignment. It does not work for this code:
  a, b, c, d, e = 1, 2, 3, 4, 5
If anybody is good enough with Emacs Lisp to handle the general case I would be interested to see it.

It's not just parallel assignment, it's destructuring assignment:

    >>> a, (b, (c, d), e) = 1, (2, [3, 4], 5)
    >>> a
    1
    >>> b
    2
    >>> c
    3
    >>> d
    4
    >>> e
    5
The question is how far you go with the highlighting.
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.