2

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
shackra
  • 277
  • 3
  • 16
  • 56
  • 1
    If referring to python-mode.el, try current trunk via bazaar, bzr branch lp:python-mode, resp. download from https://launchpad.net/python-mode – Andreas Röhler Jun 14 '13 at 06:11
  • Actually, this behavior is gone if I use ```python-mode.el```. I have some minor issues with that package now, but is nothing critical that I cannot fix, thanks! (please your comment as an answer please!) – shackra Jun 14 '13 at 15:51
  • 1
    Get used to `C-j` instead of `RET`, or bind `RET` to `newline-and-indent`. That gives you automatic indentation. –  Jun 14 '13 at 16:36
  • @JorgeArayaNavarro Even with minor issues, don't hesitate to file a report/request at `https://bugs.launchpad.net/python-mode`. This will help us and others. – Andreas Röhler Jun 15 '13 at 06:51

4 Answers4

3

You might like to try

(defun my-disable-electric-indent ()
  (set (make-local-variable 'electric-indent-mode) nil))
Stefan
  • 27,908
  • 4
  • 53
  • 82
  • Obviously, you additionally need to call this function. Or just add the `set` to your `python-mode-hook`. But if you still see the problem when `electric-indent-mode` is nil, then the problem is most likely not in electric-indent-mode. Probably in `auto-indent-mode` instead. – Stefan Jun 15 '13 at 01:55
  • @mzuther is the problem electric-indent-mode or auto-indent-mode? – samsri Sep 15 '15 at 14:44
  • If the code snippet solved his problem, then the problem was with `electric-indent-mode`, I think. – Stefan Sep 15 '15 at 20:45
  • @Sampath Kumar Rao: I haven't got `auto-indent-mode` installed, so it's definitely the `electric-indent-mode`. If it matters, my Emacs version is 24.4.1 (i686-pc-mingw32). – mzuther Sep 17 '15 at 06:38
0

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.

Andreas Röhler
  • 4,804
  • 14
  • 18
0

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

shackra
  • 277
  • 3
  • 16
  • 56
  • 1
    Congratulation! BTW several electric features are offered there. When feeling at home with the common proceeding, try `py-electric-backspace` -when editing lists for example. – Andreas Röhler Jun 15 '13 at 05:58
0

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.

xuhdev
  • 8,018
  • 2
  • 41
  • 69