"Smart" Block Copy & Paste

bboman at research.panasonic.com bboman at research.panasonic.com
Wed Oct 15 12:53:08 EDT 2003


Tim Hochberg <tim.hochberg at ieee.org> wrote in message news:<yjXib.26272$gi2.18311 at fed1read01>...
> During the recent, massive, painful Lisp-Python crossposting thread the 
> evils of Python's whitespace based indentation were once again brought 
> to light. Since Python' syntax is so incredibly brittle, and failure 
> prone, it's amazing that we don't have more editor support for our 
> feeble minds. To help reduce the severity of this crisis, I decided to 
> take a whack at creating smart block copy and paste functionality.
> 
> Anyway, I wrote some code to extract indentation information from the 
> original file when doing copies and then to match up the indentation in 
> a sensible way when doing pastes. The code is still rough and can be 
> quite slow in some situations, but I figured it was interesting enough 
> to release. If your interested in trying it out or just getting more 
> details, the code and instructions for hacking it into Idle are 
> available at http://starship.python.net/crew/hochberg/
> 
> Regargs,
> 
> -tim

I have also had trouble with editing python code.  When you copy a
block from one program to another or even move a block within a
program, you have to fix the indentation IMMEDIATELY after pasting or
else you might loose the block structure.  There are also problems
when you take out an if test, change a single nested loop to a double
nested, etc.  You can also have problems when you merge code written
with tabs and code written with spaces.

I solved all of these problem by using a special programming style.  I
did this with Emacs, but you should be able to do this with IDLE as
will with a little coding.

This method may sound strange, but it COMPLETELY ELIMINATES THE
PROBLEM.  Not only that, but if the indentation gets messed up, IT CAN
AUTOMATICALLY BE RECOVERED.  You can even delete all leading white
space from a python file and recover it.

Here is how it works:

All you have to do is make sure that every block ends with either
"pass", "return", "continue", "else", "elif", "break" or "raise". 
Since pass is defined as a noop, you can always add a pass statement
if you need to.  Here are some examples.

for i in range(10):
   x.append(i)
   continue

if x == 10:
   y += 10
   pass

if x < 0:
   raise "negative values not allowed"

for i in range(10):
  for j in range(20):
    x[i][j] = i + j
    continue
  continue

def addTwo(x):
   return x + 2

If you do this, Emacs (in python-mode) will autoindent for you as you
type.  Now when you copy a block, all you have do is press TAB at the
beginning of each line and the indentation is corrected.  The
indent-region DOES NOT WORK FOR THIS, you have to indent one line at a
time (this could be fixed by editing python-model.el).

I write all of my python programs this way.  Once you get used to it
you never have to worry about incorrect indentation again, you just
let emacs take care of it.  If you forget to put in a closing keyword
you will know immediately because Emacs won't indent as you expected.

Ideas for using in IDLE:

IDLE already knows that these keywords end blocks and will dedent when
you use them.  All you would have to do to make this work in IDLE is
to add a "fix-indentation" function.




More information about the Python-list mailing list