1

I have some lines of code in a python script opened under Py Outl mode:

last=-1
for i in range(len(page_nos)-1, -1, -1):
    if page_nos[i] != 'NULL':
        last = i
        break

if last == -1:                                                                                
    page_nos[0] = 1
    last = 0

I want to put them into a function, so I add a line of function definition in front of them:

def fillnull(page_nos):

last=-1
for i in range(len(page_nos)-1, -1, -1):
    if page_nos[i] != 'NULL':
        last = i
        break

if last == -1:                                                                              
    page_nos[0] = 1
    last = 0

I then want to indent the original lines so that they can become the body of the function. By selecting the region and M-x indent-region, the region becomes:

def fillnull(page_nos):

    last=-1
    for i in range(len(page_nos)-1, -1, -1):
        if page_nos[i] != 'NULL':
            last = i
            break

        if last == -1: 
            page_nos[0] = 1
            last = 0

Why does it put extra indents in front of the second if block?

How can I indent the original lines correctly? Thanks.

  • 1
    You can use indent-rigidly (C-x TAB) to indent the region by a fixed amount. Not sure about the mode-specific indent behavior you are seeing though. – glucas Mar 1 '15 at 14:04
  • How do you enter (C-x TAB) ? M-x indent-rigidly is sole completion. – Tim Mar 1 '15 at 14:05
  • select region & then do, M-x indent-rigidly – Madhavan Mar 1 '15 at 14:07
  • @Mad: that only put a space in front of each line – Tim Mar 1 '15 at 14:07
  • Sorry, the command is indent-rigidly. By default it is bound to the key sequence Ctrl-x followed by <tab>. You can call the command either way, then use the arrow keys to adjust the indent level. – glucas Mar 1 '15 at 14:07
3

Caused as py-indent-region calculated indent of every single line. Which results in outmost indent of each line.

As this may destroy program-logic, fixed in trunk following

https://bugs.launchpad.net/python-mode/+bug/1426903

New behavior:

If first line of regions indent is changed, remaining lines' indent is kept by default.

If first lines' indent is unchanged, the remaining are calculated line-by-line, also With C-u option.

  • Thanks. how can I fix my emacs problem? – Tim Mar 17 '15 at 20:19

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.