3

Right now, in emacs' python-mode line continuations are aligned to the end of the previous line, as follows:

this_is_a_list_of_django_urls = ('',
                                 url(r'^admin/?', include(admin.site.urls)),
                                 url(r'^polls/?', include('polls.urls'))
                                )

But I find the above to be pretty ugly. Is there any way to configure emacs' python-mode to autoindent like this:

this_is_a_list_of_django_urls = ('',
    url(r'^admin/?', include(admin.site.urls)),
    url(r'^polls/?', include('polls.urls'))
)

I find the second version to be much easier to read, and so I'd like hitting TAB to only indent by one level, rather than however much it takes to align with the end of the previous line.

2
  • Thats actually interesting request, from code I don't see it is configurable now, you may send a feature-request to emacs-devel. M-x report-emacs-bug.
    – kindahero
    Commented May 31, 2013 at 8:38
  • You should accept Andreas' solution and put the quotes on a separate line as well. Then emacs will indent them without aligning. Commented Nov 22, 2015 at 18:18

2 Answers 2

2

PEP8 says:

No:

Arguments on first line forbidden when not using vertical alignment

foo = long_function_name(var_one, var_two,
    var_three, var_four)

WRT closing parenthesis python-mode.el meanwhile offers a choice, boolean `py-close-at-start-column-p', default is nil.

When non-nil, it will be lined up under the first character of the line that starts the multi-line construct, as in:

my_list = [
    1, 2, 3,
    4, 5, 6,
]
1
  • Shows that PEP8 has its good sides. I've been doing that for years, it seemed natural to me to indent in this way. I wonder how anyone can think that the aligned version with parameters on the first line is better. Why treat some parameters different from the rest? Commented Nov 22, 2015 at 18:16
1

Not a solution, really, but if you put the first element of the tuple on a new line, you get almost the behavior you want out-of-the-box.

this_is_a_list_of_django_urls = (
    '',
    url(r'^admin/?', include(admin.site.urls)),
    url(r'^polls/?', include('polls.urls'))
    )

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .