How to align second line of command with first line?

Asked by holger (lp)

I have a command that extends over two (or more) lines, e.g.

do_something(argument1,
  argument2)

How do I align the second line such that argument2 is right below argument1? That is usually (in other programming modes) done by hitting Tab with the cursor in line 2, but Tab just reduces the indent until argument2 is in column 0. The next Tab aligns it the way I want it. Tab maps to py-indent-or-complete, which expands to an endless chain of more commands.

Is this a setting? Or should I use an other key instead of Tab?

Question information

Language:
English Edit question
Status:
Solved
For:
python-mode.el Edit question
Assignee:
No assignee Edit question
Solved by:
holger (lp)
Solved:
Last query:
Last reply:
Revision history for this message
Andreas Roehler (a-roehler) said :
#1

If a "def" precedes, arguments should be indented as asked for.

Revision history for this message
holger (lp) (holger-lp) said :
#2

No, I mean within a called function, not the function arguments itself:

### Not here:
def do_something(argument1, argument2):
    ### But here:
    do_something_else(argument1,
     argument2)

I figured out that it works if I move the cursor to column 0 first. But what does it do if the cursor is right before the a of argument2? For dedent I can use backspace, tab should align the current line with the previous line.

I get the same behaviour for the function definition:

def do_something(argument1,
 argument2):
    pass

argument2 is dedented if the cursor is right before the a of argument2. Same for the alignment of "pass".

Revision history for this message
Andreas Roehler (a-roehler) said :
#3

TAB is implemented to cycle possible indents. If at outmost possible indent, first TAB should do nothing, second would dedent until 0 is reached, than jump to the outmost again.

Revision history for this message
holger (lp) (holger-lp) said :
#4

A typical case where I need the align functionality is after a function name was changed, and the new name is longer than the old name. All following lines with arguments that belong to that function now have not enough indentation.

My original question was how the alignment can be done. I mean without adding the required spaces manually. And without the TAB cycle that starts with dedent. The shortest sequence I found so far is Home+Tab. With python.el that ships with Emacs just one TAB is needed.

Revision history for this message
Andreas Roehler (a-roehler) said :
#5

Am 03.08.2015 um 13:56 schrieb holgertrans:
> Question #269822 on python-mode.el changed:
> https://answers.launchpad.net/python-mode/+question/269822
>
> Status: Answered => Open
>
> holgertrans is still having a problem:
> A typical case where I need the align functionality is after a function
> name was changed, and the new name is longer than the old name. All
> following lines with arguments that belong to that function now have not
> enough indentation.

Call py-indent-and-forward for the remaining lines

Alternatively there are a couple of
py-mark-... commands

usable

which follow by py-indent-region

>
> My original question was how the alignment can be done. I mean without
> adding the required spaces manually. And without the TAB cycle that
> starts with dedent.

It only dedents from second TAB.
First TAB should indent unless it is at max-indent already

> The shortest sequence I found so far is Home+Tab.
> With python.el that ships with Emacs just one TAB is needed.
>

Revision history for this message
holger (lp) (holger-lp) said :
#6

TAB, py-indent-or-complete, and py-indent-line dedent immediately if the cursor is not in column 0.

bolp seems to control whether outmost indent is used ("now choose the indent" in py-indent-line). That correlates with my observation that the cursor must be in column 0 to get the outmost indent. I added the case that this-command is different from last-command, now it works for me.

Revision history for this message
Andreas Roehler (a-roehler) said :
#7

Am 04.08.2015 um 11:12 schrieb holgertrans:
> Question #269822 on python-mode.el changed:
> https://answers.launchpad.net/python-mode/+question/269822
>
> Status: Answered => Solved
>
> holgertrans confirmed that the question is solved:
> TAB, py-indent-or-complete, and py-indent-line dedent immediately if the
> cursor is not in column 0.

Not quite. They dedent immediately only if _not_ at outmost position.
> bolp seems to control whether outmost indent is used ("now choose the
> indent" in py-indent-line). That correlates with my observation that
> the cursor must be in column 0 to get the outmost indent.

Yes, that's the direction of cyling.

> I added the
> case that this-command is different from last-command, now it works for
> me.
>