improper exiting from indentation in emacs python-mode

ε祈祈猫儿з 提交于 2019-12-22 04:38:21

问题


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.


回答1:


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



回答2:


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.




回答3:


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




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/9071995/improper-exiting-from-indentation-in-emacs-python-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!