3
votes

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!

1
I don't know the answer for your question, but for the definitions thing, I have a couple of suggestions. Firstly, turn on eldoc-mode, which gives you a list of arguments for the current function. Secondly, use C-h v or C-h f (variables/functions) with point on top of the thing you don't recognise. Usually one of them will find something and there's often documentation or a source link. Finally, symbol-start is a special name used by the "rx" function. - Rupert Swarbrick
@Rupert Swarbrick: Thanks for the feedback Rupert. - Ben

1 Answers

3
votes

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.