Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs Python.el, Syntax Highlighting Quirks

I'm using python.el version 0.23.1 for Emacs right now. The syntax highlighting seems to be a bit off -- any variable name containing an underscore followed by a keyword will result in the keyword being highlighted. Example, "foo_list" will result in "list" being highlighted.

More for my own understanding of Emacs-Lisp than anything (it's not a big deal) how do I go about fixing that. Here is where I think the relevant code is in "python.el", lines 312-318. I suspect the problem is with the definition of "symbol-start" but I have no idea what that name refers to.

(defvar python-font-lock-keywords
  ;; Keywords
  `(,(rx symbol-start
         (or "and" "del" "from" "not" "while" "as" "elif" "global" "or" "with"
             "assert" "else" "if" "pass" "yield" "break" "except" "import"
         "print" "class" "exec" "in" "raise" "continue" "finally" "is"
         "return" "def" "for" "lambda" "try" "self")
     symbol-end)

One thing I've struggled with in Emacs-Lisp so far is that I am finding it difficult in situations like these to follow names back to their definitions.

Thanks in advance!

like image 638
Ben Avatar asked Aug 27 '26 11:08

Ben


1 Answers

When you say you're using python-mode 0.23.1, do you mean the one that comes bundled with Emacs or this one: http://launchpad.net/python-mode ? (which seems to be on version 6.something)

The reason I'm asking is that I can't reproduce what you're seeing. In an empty python buffer, I inserted

def x ():
    a_list =3

and "list" is only highlighted when I delete the "a_". I'm using the version bundled with Emacs, with a snapshot version of Emacs so this might be the difference?

Incidentally, the font lock rule that you quote looks right to me: maybe the problem is that in your version "_" isn't set to have symbol syntax? You can check by typing

M-: (string (char-syntax ?_))

when in a python buffer. You should get "_" (which means symbol). If you get "." (punctuation) or something else weird, that probably explains what's gone wrong.

like image 112
Rupert Swarbrick Avatar answered Aug 30 '26 01:08

Rupert Swarbrick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!