Search code examples
pythonemacsprogress

emacs 24: How do I get back emacs-23 sexp behavior for python mode?


I'm running both emacs 23 and 24 with "--no-init-file" to avoid loading my customizations. But then I explicitly load my ancient version of python-mode.el (version 3.105, last copyright notice 1998), and see the same behavior I'm about to describe, so I believe it's core emacs, not python.

Consider this code (the * marks the cursor location):

*def emacs_is_fun():
    for x in range(3):
        print(x)

Put the cursor at column 4 at the start of "def".

With emacs 23, running mark-sexp selects "def".

With emacs 24, mark-sexp selects the entire code block, to the end of the print statement.

This isn't bad. The main problem is this common usage:

    *very_long_var_name[very_long_expn] += long_function_call(long_arg_list, blah)

Again, the cursor is at the start of the printable part of the line, col 4.

In emacs 23, mark-sexp selects very_long_var_name.

In emacs 24, mark-sexp selects the full line, from col 4 to the end.

I've been using Ctrl-alt-space alt-w to quickly save variable names. (Or alt-2 ctrl-alt-space alt-w to save a ref expression.) Now it's gone. It's emacs, so this must be customizable, but I haven't found where the new behaviour is implemented (and remember I'm using a python-mode that is about 15 years old). What do I need to put in my .emacs file?


Solution

  • @Eric Interesting. Thanks for the context in the comment of my other answer.

    I think you may just want this then:

    (add-hook 'python-mode-hook (lambda () (setq forward-sexp-function nil)))

    From comments inside python-mode built-in emacs 24 (which I think you may be getting instead)

    At last but not least the specialized python-nav-forward-sexp allows easy navigation between code blocks. If you prefer cc-mode-like forward-sexp movement, setting forward-sexp-function to nil is enough...

    This gels closer to what I think of a sexp as, like mentioned in my other answer, but outside of lisp a sexp seems at least somewhat ambiguous.