0

This is (yet) another question related to electric-indent-mode and Python, but since I couldn't find what I address here in the other ones, here we go.

I find the behavior of electric-indent-mode quite useful in general, but there is a minor thing that annoys me:

Say we have the following buffer, where | (pipe) represents the cursor position:

def example():
    if True:
        print("Electricity")

    |

With electric-indent-mode on, pressing RET results in the following state:

def example():
    if True:
        print("Electricity")


        |

The automatic indentation of the newline regards only the first non-empty (i.e., that consists not only of whitespace) previous line. However, what I would like is to have it take into account the cursor position of the previous line if the previous line consists only of whitespace (and after pressing enter, still remove the whitespace on the previous line afterwards if there was any):

def example():
    if True:
        print("Electricity")


    |

How can I tweak electric-indent-mode configurations to achieve the desired behavior? Conversely, what are the possible solutions (regardless of using electric-indent-mode or not)?

1

Typing [control j] instead of RET should jump to the beginning of line when electric-indent-mode is on. If EIM is off, RET jumps to the BOL and C-j to indent.

  • Thank you @Andreas Röhler. The problem is, however, that I am looking for a solution that works regardless of the indentation level we are in (and that does not require one to memorize or switch between keys). I have updated my question with a slightly different example to make the point clearer. Maybe this answer has some hints on how to solve it btw. – Arthur Colombini Gusmão Oct 17 '19 at 19:11
  • 1
    @ArthurColombiniGusmão Don't understand saying "to have it take into account the indentation of the previous line, even if it was an empty line" - how could an empty line have an indentation? – Andreas Röhler Oct 18 '19 at 6:49
  • sorry, I should have been more clear. By that I mean a line composed only of whitespace (e.g., spaces or tabs). Basically what I want to achieve is to have Emacs preserve the indentation level I had in the previous line, even if there was no non-whitespace character in it. I will update my question. – Arthur Colombini Gusmão Oct 18 '19 at 12:32
  • 1
    @ArthurColombiniGusmão IMO that may disturb users, as whitespace commonly isn't visible. Emacs would indent then after rules which are hard to follow. – Andreas Röhler Oct 19 '19 at 17:03
  • 1
    @ArthurColombiniGusmão Okay, but that's like newline-and-indent is expected to behave. If not, please consider a bug-report - said as a non-electric user, as electricity more often breaks my workflow than it helps. – Andreas Röhler Jan 30 at 8:04
1

I see the need for this every day. The current behavior is really annoying, to the point of distracting me from the flow of writing code productively.

I have entered the previous code:

def example():
    if True:
        print("Electricity")

Entering a newline lines up the next line with the beginning of the print statement. To start a new definition, I have to hit TAB three times to get the cursor to the left margin. Then I realize I want a blank line, so I hit newline. This puts the indent back at the "print" level. 3 more TABS, then enter a comment:

# The following def does ...

Hitting newline then indents the comment to the "print" level.

def example():
    if True:
        print("Electricity")

        # The following def does ...

Now, position the cursor at the comment's #, hit M-\ (or 3 tabs), and the comment is at the left margin. Move to next line, hit M-\ (or 3 tabs) to get to left margin, and type:

def foo():

hitting newline indents the "def" to the "print" level, leaving the preceding comment at the left margin.

def example():
    if True:
        print("Electricity")

# The following def does ...
        def foo():

Move to the next line, hit enough tab characters to get to the left margin, and we're ready to start typing the def's body. At this point, the indentation stays where I want it to be, unless, after modifying the def's header, I hit newline, which will indent the def back to the "print" level. This happens even if there is a multi-line comment block before the "def"!

So, that's at least 3 instances of unwanted indentation, along with 9 tabs to undo the unwanted behavior, namely, lining up the indent with the previous line, especially when the previous line has been indented correctly, manually, by me.

@ArthurColombiniGusmão suggests that this will disturb users. Sure, it might. But it's a lot less disturbing to have properly-indented lines un-indented multiple times. If I end up with an unexpected indentation because the previous line was indented for some other reason, I should be able to fix it (by hitting TAB enough times), and know that my "def" won't be unwantedly indented, and not have that work undone by hitting newline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.