【问题标题】:improper exiting from indentation in emacs python-mode在emacs python-mode中不正确地退出缩进
【发布时间】:2012-02-22 17:26:22
【问题描述】:

我正在使用 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 语句主体)并按退格键时,它会在缩进中跳出一级,而不是退格。例如,如果我有:

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 -nw”运行 emacs 时,我无法让它工作(我正在远程登录到服务器并且不想启动 X 界面)。当我删除“-nw”并远程使用较慢的 X 接口的 emacs 时,一切正常……知道为什么会这样吗?会不会是与退格或类似问题相关的 shell 配置问题?

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

【问题讨论】:

    标签: python emacs elisp python-mode


    【解决方案1】:

    对于现代版本的 Emacs,use python.el, not 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
    

    【讨论】:

    • 我在 linux 上使用 GNU Emacs 23.1.1(GTK+ 版本 2.20.1),但它的行为不是这样。我从 .emacs 中删除了你提到的内容,重新加载,即使我默认获得语法突出显示和一些缩进,退格仍然对缩进不敏感,只是常规退格。
    • 那么听起来你的 .emacs 中的其他东西正在干扰。尝试使用 emacs --no-init-file 启动 emacs。当我以这种方式启动 emacs 时,退格键在 Ubuntu 11.10、GNU Emacs 23.3.1(GTK+ 版本 2.24.5)上正常工作。
    • 它在我的情况下不起作用,即使没有初始化文件,我也没有得到适当的缩进
    • 我认为stackoverflow.com/questions/157018/emacs-and-python/… 现在不再成立了。 python-mode.el 支持三引号,启动ipython集成等。从去年开始,它的开发非常活跃。
    【解决方案2】:

    我的猜测是 python-mode.el 不支持不同的缩进宽度。您的最后一个示例使用 2 个 3 个空格进行缩进。为什么不总是在每个缩进中使用 4 个空格?使用4个空格是standard style

    已编辑:当我打开一个新文件并复制您的上一个示例时,我可以重现您所说的内容。但是,当我关闭它并重新打开它时,我可以正确退出缩进。我可以通过选择代码并在不关闭/重新打开的情况下点击C-c : (py-guess-indent-offset) 来做同样的事情。

    【讨论】:

    • 我不认为它与 # 缩进空格有关,因为当我从头开始一个新文件并且问题仍然存在时,这是由 python/emacs 模式决定的。如果我在没有 -nw 标志的情况下运行 emacs,问题就会消失,这让我认为这是 Mac OS X shell 和远程服务器之间的交互。你怎么看?
    • 在终端中,某些键序列的作用不同。我不确定,但如果我没记错的话,TAB 就是其中之一。有一种方法可以解决这个问题,但可能 python-mode 没有实现它。
    【解决方案3】:

    这是一个错误。

    py-smart-indentation 最近一直在错误地猜测值 4。

    为了快速解决问题,请在以下位置报告 python-mode.el 错误:

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

    【讨论】:

      【解决方案4】:

      固定在当前主干中

      使用集市获得它:

      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

      【讨论】:

        【解决方案5】:

        不用退格,只需按&lt;tab&gt;。它应该跳转到下一个逻辑缩进位置。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-10-05
          • 1970-01-01
          • 2010-10-14
          • 2011-06-06
          • 1970-01-01
          • 1970-01-01
          • 2011-11-05
          相关资源
          最近更新 更多