电子缩进模式破坏了我的Python代码

时间:2013-06-13 19:16:29

标签: emacs elisp python-mode

我在启用了python-mode的Emacs上有此代码:

def func(a):
    if a:
        return True
    return False

当我在return Falsedef func(之间移动光标时,代码会自动缩进,打破它:

def func(a):
    if a:
        return True
        return False #Oops!

我发现这是因为electric-indent-mode,一种次要的全球模式。但是,我试图将其关闭,但问题仍然存在。

我使用的elisp代码是:

(defun disable-electric-indent ()
  (set (make-local-variable 'electric-indent-functions)
       (list (lambda (arg) 'no-indent))))

以及我的python-mode-hook看起来如何:

(add-hook 'python-mode-hook
          (lambda ()
              (flyspell-prog-mode)
              (auto-complete-mode)
              (nlinum-mode)
              (toggle-truncate-lines t)
              (setq autopair-handle-action-fns
                    (list 'autopair-default-handle-action 'autopair-python-triple-quote-action))
              (centered-cursor-mode)
              (auto-indent-mode)
              (autopair-mode)
              (column-marker-1 80)
              (flycheck-mode)
              (setq ac-auto-start 2)
              (disable-electric-indent) ;; esto deberia hacer que emacs deje de romper las pelotas con el codigo en python
              (jedi:setup)))

如果我关闭auto-indent-mode,则此行为会停止(但是,我不会自动缩进,glol)。

我的emacs版本:GNU Emacs 24.3.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.8.1) of 2013-04-29 on eric

编辑:根据melpa,我正在使用python包(内置Python对Emacs的飞行马戏支持,你知道)0.24.2 。也许我应该删除它并在其6.0.10版本中使用python-mode包?

4 个答案:

答案 0 :(得分:3)

您可能想尝试

(defun my-disable-electric-indent ()
  (set (make-local-variable 'electric-indent-mode) nil))

答案 1 :(得分:0)

假设有一个错误,它不应该像您的示例所示那样缩进。

除了预期来自自动缩进模式的几个冲突之外,认为使用python-mode是一件坏事。不是在你的例子中,但在其他地方有缩进只是一个选择。自动缩进不知道在哪里缩进。 作为换行和缩进,它将选择最可能的,在某些情况下这将是错误的。这可能会变得令人讨厌。

答案 2 :(得分:0)

我刚迁移到python-mode.el并保留代码以关闭electric-indent-mode:)

答案 3 :(得分:0)

当不应该发生重新注册时,你应该在任何模式下使用(setq electric-indent-inhibit t),例如python模式。这是正式的方式,作为C-h v electric-indent-inhibit中的文件。

相关问题