improper exiting from indentation in emacs python-mode

1.6k views Asked by At

I am using Emacs python-mode. I invoke it using this in my .emacs

(add-to-list 'load-path "~/emacs/python-mode.el-6.0.3/")
(autoload 'python-mode "python-mode" "Python Mode." t)
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
(add-to-list 'interpreter-mode-alist '("python" . python-mode))
(require 'python-mode)
(add-hook 'python-mode-hook
      (lambda ()
    (set-variable 'py-indent-offset 4)
    ;(set-variable 'py-smart-indentation nil)
    (set-variable 'indent-tabs-mode nil)
    (define-key py-mode-map (kbd "RET") 'newline-and-indent)
    ;(define-key py-mode-map [tab] 'yas/expand)
    ;(setq yas/after-exit-snippet-hook 'indent-according-to-mode)
    ))

It generally indents okay, in that if I wrote:

if condition:

and then pressed Return, it would properly put the cursor in the indented newline. The problem is that it doesn't exit out of indentation correctly. In other systems, when I make a new line in the body of the indented clause (like the if statement body) and press Backspace, it jumps one level out in indentation, instead of backspacing. For example, if I had:

if condition: 
  statement1
  statement2

and I pressed return, backspace, after statement2, it would put the cursor here:

if condition: 
  statement1
  statement2
<-- cursor position

If you have many idented levels and it doesn't do this, editing Python becomes impossible, since you have to backspace manually until you get to the right indentation level... it's error prone and annoying, e.g. if you had:

for something:
  for other:
    if hello:
      while x:
         statement1
         <-- How to indent back to level of "for other"?

EDIT: I was unable to get this to work while running emacs as "emacs -nw" (I am logging in to a server remotely and don't want the X interface to start). When I remove "-nw" and use emacs, with the slower X interface remotely, it all works... any idea why this might be? Could it be a shell configuration issue related to backspaces or something like that?

How can this be fixed? I just want it to backspace at indent levels if I'm inside a clause. thanks.

5

There are 5 answers

4
unutbu On

For modern versions of Emacs, use python.el, not python-mode.el. If you remove all those lines from your .emacs, I think python.el will be enabled by default, and you'll get a Python mode when you open a .py file.

I tested the blank default setting on my system and backspace properly goes back one level of indention in your example.

So the answer seems to be, "do nothing!" :)


On Ubuntu, python.el is provided by the emacs23-common package which is part of the standard emacs installation:

% dpkg -S /usr/share/emacs/23.3/lisp/progmodes/python.elc
emacs23-common: /usr/share/emacs/23.3/lisp/progmodes/python.elc
0
Sean Perry On

Instead of backspacing, just press <tab>. It should jump to the next logical indent location.

0
Andreas Röhler On

This is a bug.

py-smart-indentation has wrongly been guessing a value of 4 lately.

To get things fixed quickly, please report python-mode.el bugs at:

https://bugs.launchpad.net/python-mode

0
Andreas Röhler On

fixed in current trunk

get it using bazaar:

bzr branch lp:python-mode

or via html download-button

https://launchpad.net/python-mode resp. http://bazaar.launchpad.net/~python-mode-devs/python-mode/python-mode/files

2
tkf On

My guess is that python-mode.el does not support having different indentation width. Your last example uses 2 and 3 spaces for indentation. Why don't you use 4 spaces per indentation always? Using 4 spaces is standard style.

EDITED: When I open a new file and copied your last example I could reproduce what you said. However, when I close it and reopened it, I could exit from the indentation correctly. I could do the same thing by selecting the code and hit C-c : (py-guess-indent-offset) without close/reopen.