在 emacs python-mode 中不正确地退出缩进
我正在使用 Emacs python 模式。我在 .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)
))
它通常可以缩进,因为如果我写:
if condition:
然后按回车键,它会正确地将光标放在缩进的换行符中。问题是它无法正确退出缩进。在其他系统中,当我在缩进子句的正文中创建一个新行(如 if
语句正文)并按 Backspace 时,它会在缩进中跳出一级,而不是退格。例如,如果我有:
if condition:
statement1
statement2
并且在statement2
之后按了回车键、退格键,它会将光标放在这里:
if condition:
statement1
statement2
<-- cursor position
如果您有许多已识别的级别并且它不这样做,则编辑Python变得不可能,因为你必须手动退格,直到到达正确的缩进级别...这很容易出错并且很烦人,例如,如果你有:
for something:
for other:
if hello:
while x:
statement1
<-- How to indent back to level of "for other"?
编辑:在将 emacs 运行为“时,我无法让它工作” emacs -nw" (我正在远程登录服务器,并且不希望启动 X 界面)。当我删除“-nw”并使用 emacs 时,远程使用较慢的 X 界面,一切正常......知道为什么会这样吗?这可能是与退格键或类似问题相关的 shell 配置问题吗?
如何解决这个问题?如果我在子句中,我只想让它在缩进级别退格。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于现代版本的 Emacs,使用 python.el,而不是 python-mode.el。
如果您从 .emacs 中删除所有这些行,我认为 python.el 将默认启用,并且当您打开 .py 文件时,您将获得 Python 模式。
我在我的系统上测试了空白默认设置,在您的示例中,退格键可以正确返回一级缩进。
所以答案似乎是“什么都不做!” :)
在 Ubuntu 上,python.el 由 emacs23-common 包提供,该包是标准 emacs 安装的一部分:
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:
我的猜测是 python-mode.el 不支持不同的缩进宽度。您的最后一个示例使用 2 个和 3 个空格进行缩进。为什么每个缩进不总是使用 4 个空格?使用 4 个空格是标准样式。
编辑:当我打开一个新文件并复制您的最后一个示例时,我可以重现您所说的内容。但是,当我关闭它并重新打开它时,我可以正确退出缩进。我可以通过选择代码并点击
Cc :
(py-guess-indent-offset) 来完成同样的操作,而无需关闭/重新打开。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.这是一个错误。
py-smart-indentation 最近错误地猜测了值 4。
要快速修复问题,请在以下位置报告 python-mode.el 错误:
https://bugs.launchpad.net /python-模式
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
只需按
,即可代替退格键。它应该跳转到下一个逻辑缩进位置。Instead of backspacing, just press
<tab>
. It should jump to the next logical indent location.已修复在当前主干中
,使用 bazaar:
bzr Branch lp:python-mode
或通过 html
download-button
https://launchpad.net/python-mode
分别。
http://bazaar.launchpad.net/~python -mode-devs/python-mode/python-mode/files
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