"Smart" Block Copy & Paste

T

Tim Hochberg

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
 
S

Steve Lamb

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.

Why, exactly, would we need more than << and >>?
 
T

Tim Hochberg

Steve said:
Why, exactly, would we need more than << and >>?

I assume that's block dedent and indent? You don't, really, need more
than that.

However, many editors tend to mess up the indentation on the first line
of a pasted block, which as to be corrected separately from the rest.
You can usually avoid this if your careful or hit home/ctrl-a/etc before
pasting, but that's extra keystrokes. Also, at least some editors
(SciTe, Idle) don't set the mark on your pasted text, so then you need
to select the pasted text before (in)|(de)denting it.

Of course what I did was probably massively overkill for these problems,
but it's an interesting project. Also, the behaviour of Idle with the
patch is overall better than without, IMO. In truth, I suspect that I
could get a similar gain with less invasive and simpler changes, but it
wouldn't be as intersting.

Regards,

-tim
 
B

bboman

Tim Hochberg said:
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[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.
 

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top