7

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 Answers5

5

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
Community
  • 1
  • 1
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • I am using GNU Emacs 23.1.1 on linux (GTK+ Version 2.20.1) and it does not behave this way. i removed the stuff you mentioned from .emacs, reloaded and even though i get syntax highlighting and some indentation by default, the backspace is still insensitive to indents and is just regular backspace. –  Feb 01 '12 at 06:33
  • Then it sounds like something else in your .emacs is interfering. Try starting emacs with `emacs --no-init-file`. When I start emacs this way, the backspace works properly on Ubuntu 11.10, GNU Emacs 23.3.1 (GTK+ version 2.24.5). – unutbu Feb 01 '12 at 10:34
  • It does not work in my case, even with no init file, I do not get proper indents –  Feb 02 '12 at 04:49
  • 1
    I think http://stackoverflow.com/questions/157018/emacs-and-python/157074#157074 does not hold anymore now. python-mode.el supported triple quote, started ipython integration, etc. Its development got highly active since last year. – tkf Feb 09 '12 at 21:28
1

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

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
1

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

Sean Perry
  • 3,776
  • 1
  • 19
  • 31
1

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.

tkf
  • 2,990
  • 18
  • 32
  • I don't think it has to do with # spaces for indentation, since that's dictated by python/emacs mode when I start a new file from scratch and the problem persists. The problem goes away if I run emacs without the `-nw` flag which makes me think it's an interaction between Mac OS X shell and the remote server. What do you think? –  Jul 13 '12 at 13:06
  • In terminal, some key sequences act differently. I am not sure, but if I remember correctly, TAB is one of those. There is a way to workaround this, but probably python-mode does not implement it. – tkf Jul 13 '12 at 13:34
0

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

karthikr
  • 97,368
  • 26
  • 197
  • 188