Re: Automated indentation

Tim Peters (tim@ksr.com)
Mon, 09 May 94 03:20:22 -0400

I'm always surprised when people gripe about the indentation! I don't
mind the "end if" etc business, but in the meantime:

> [george]
> ... For example when using python-mode in emacs, I hit tab on a line by
> mistake and that line gets indented 5 levels. Where was it originally?

`C-x u' (advertised-undo) restores it to its original position.

> As a program is evolving (as opposed to simply reading the code) one
> is often cutting, pasting and otherwise modifying code quite
> frequently. My experience is that I am constantly annoyed at having to
> re-indent every line when the indentation could be automated since I
> already did it correctly once.

Study the pymode docs for

`C-c TAB' py-indent-region
`C-c C-b' py-mark-block
`C-c <' py-shift-region-left
`C-c >' py-shift-region-right

For example, suppose you want to get rid of the 'if' clause in

n = n + 2

if n < psq:
for fac in facs:
if n % fac == 0:
break
else:
primes.append(n)
suspend(n)

psq = p*p

1) Stick point anywhere on the `if' line.

2) Do `C-c C-b'. The entire 'if' block is then marked as the current
region, and point moves to the 1st column of the `if' line.

3) Do `C-k C-k'. This deletes the `if' line.

4) Do `C-c TAB'. What was the body of the `if' block shifts 4 columns
left, to align with "n = n + 2".

Or suppose you have

n = n + 2
psq = p*p

in file A, have

def whatever(fac,facs,n):
for fac in facs:
if n % fac == 0:
break
else:
primes.append(n)
suspend(n)
return 1

in file B, and want to copy the `for' structure after the `n = n + 2'
line, and under the control of a new `if n < psq:' line.

1) In file A, go to the end of `n = n + 2' and do
LFD
if n < psq:
RETURN

2) Switch to file B, put point anywhere on the `for' line, and do
C-u C-c C-b
to mark the entire for/else structure.
Then the usual Emacs `M-w' (copy-region-as-kill) to copy it into
the yank buffer.

3) Switch back to file A, and do
C-y (Emacs yank; pastes the insanely indented for/else structure)
C-c TAB (reindents the structure wrt to the `if ...:')

If py-indent-offset is 4, you end up with

n = n + 2
if n < psq:
for fac in facs:
if n % fac == 0:
break
else:
primes.append(n)
suspend(n)

psq = p*p

I.e., so long as you're using Emacs, there's _never_ a reason to
"re-indent every line" by hand! pymode's `[C-u] C-c C-b' and `C-c TAB'
are pretty smart, but do take some getting used to. pymode's `C-c <' and
`C-c >' are stupid as nails, but can be used without surprise instantly
(they shift the current region left or right (respectively) rigidly, by
py-indent-offset columns, or by any other number of columns you specify
with a prefix arg). Note too that the region-shifting functions maintain
the current region, so can be chained (e.g., if `C-c TAB' doesn't get you
to exactly where you wanted to be, it can be followed immediately by a
`C-c <' (whatever) to fine-tune it).

Ironically enough, I suspect it won't really be much easier to cut/paste
if Guido does implement the "end if" etc gimmicks. It _certainly_ won't
be easier if pymode isn't taught about them too <wink>.

waiting-for-someone-to-exclaim-that-the-'end'-word-must-line-up-
under-the-block's-body-instead<0.4-grin>-ly y'rs - tim

Tim Peters tim@ksr.com
not speaking for Kendall Square Research Corp