shackra
shackra

Reputation: 366

electric-indent-mode breaks my Python code

I have this code on Emacs with python-mode enabled:

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

When I move the cursor between return False and def func( the code is automatically indented, breaking it:

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

I came to know that this happens because of electric-indent-mode, a minor global mode. However, I tried to turn it off, but the issue remains.

The elisp code that I use is this:

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

and this how my python-mode-hook looks:

(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)))

If I turn off auto-indent-mode this behavior stops (however, I don't get auto indentation, glol).

my emacs version: GNU Emacs 24.3.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.8.1) of 2013-04-29 on eric

EDIT: I'm using the python package (the built-in Python's flying circus support for Emacs, you know) in its version 0.24.2, according to melpa. Maybe I should remove it and use python-mode package in its version 6.0.10?

Upvotes: 2

Views: 1867

Answers (4)

xuhdev
xuhdev

Reputation: 9323

You should use (setq electric-indent-inhibit t) in any mode when reindentation is not supposed to happen, such as python mode. It is the official way to do it, as document in the C-h v electric-indent-inhibit.

Upvotes: 0

shackra
shackra

Reputation: 366

I just migrated to python-mode.el and leave the code to turn off the electric-indent-mode :)

Upvotes: 0

Andreas Röhler
Andreas Röhler

Reputation: 4804

Assume there is a bug, it should never be indented as your example shows.

Beside would expect several conflicts from an auto-indent-mode, consider it a bad thing with python-mode. Not in your example, but at other places there is indent just a choice. Auto-indent can't know where to indent then. As newline-and-indent, it will select the outmost probably, which will be wrong in some cases. That might turn nasty.

Upvotes: 0

Stefan
Stefan

Reputation: 28521

You might like to try

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

Upvotes: 3

Related Questions