在emacs python模式中不正确退出缩进 [英] improper exiting from indentation in emacs python-mode

查看:181
本文介绍了在emacs python模式中不正确退出缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Emacs的python模式。我在我的 .emacs

 (附加到-list'load-path〜/ emacs / python-mode.el-6.0.3 /)
(autoload'python-modepython-modePython Modet)
add-to-list'auto-mode-alist'(\\.py\\。python-mode))
(add-to-list'interpreter-mode-alist' python)python-mode
(需要python)
(add-hook'python-mode-hook
(lambda()
'py-indent-offset 4)
;(set-variable'py-smart-indentation nil)
(set-variable'indent-tabs-mode nil)
(define-key py -mode-map(kbdRET)'newline-and-indent)
;(define-key py-mode-map [tab]'yas / expand)
;(setq yas /
))

它是根据模式退出代码段一般缩进可以,如果我写道:

  if condition:

然后按Return键,会正常将光标放在缩进的换行符中。问题是它没有正确地退出缩位。在其他系统中,当我在缩进子句的主体中创建新行(如 if 语句正文)并按Backspace时,它会在缩进中跳出一个级别的后支。例如,如果我有:

  if condition:
statement1
statement2

,我按下return,backspace,在 statement2 之后,光标在这里:

  if condition:
statement1
statement2
< - cursor position

如果您有很多标识级别,但不执行此操作,则无法编辑Python,因为您必须手动退格,直到你达到正确的缩进级别...它的错误倾向和烦人,例如如果你有:

 的东西:
其他:
如果你好:
x:
statement1
< - 如何缩进为其他的级别?

编辑:在运行emacs时,我无法使其工作emacs -nw(我正在远程登录服务器,不希望X接口启动)。当我删除-nw并使用emacs,与较慢的X接口远程,这一切都可以...任何想法为什么这可能是?这可能是与后台空间相关的shell配置问题吗?



如何解决?如果我在一个子句内,我只是希望它在缩进级别退格。谢谢。

解决方案

对于现代版本的Emacs,使用python.el,而不是python-mode.el
如果从.emacs中删除所有这些行,我认为默认情况下会启用python.el,当您打开.py文件时,您将得到一个Python模式。



我测试了我的系统上的空白默认设置,退格正确地返回了您的示例中的一个缩进级别。



所以答案似乎是, 没做什么! :)






在Ubuntu中,python.el由emacs23-common包提供,它是标准emacs安装的一部分:

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


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.

解决方案

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

这篇关于在emacs python模式中不正确退出缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆