Subject: Python mode: "eval region" indentation problem

From: "Hendrik Weisser" <[email-masked]>
Date: Tue, 17 Sep 2013 14:42:54 +0200 (CEST)

Dear developers,

The "eval region" function in Emacs' Python major mode (python.el) has problems dealing with indented code.
Consider this example:

1 for i in range(10**10):
2     do_something(i)
3     do_more(i)
4 

When testing such code, it is often useful to evaluate the loop contents for an example value of the parameter, rather than running the whole loop. In the example, I would like to be able to define a region consisting of lines 2 and 3, and run them using the "eval region" command (after setting a value for "i" in the interpreter). However, this will give me an "IndentationError: unexpected indent" in Python (either for the first or the second line, depending on whether the region starts at the beginning of line 2 or after the indent).

It should be possible to resolve such cases by stripping any leading whitespace from the first line in the region, and stripping the same amount from each following line (provided that the region starts at the beginning of the line). I believe this behaviour would be quite intuitive.

Please consider adapting the "eval region" function in this way to increase its utility.

Best regards

Hendrik