Settings

Theme

Python Mode in Emacs

emacswiki.org

27 points by tomwans 15 years ago · 3 comments

Reader

cgrubb 15 years ago

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.
  • yangyang 15 years ago

    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.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection