"Smart" Block Copy & Paste

Discussion in 'Python' started by Tim Hochberg, Oct 14, 2003.

  1. Tim Hochberg

    Tim Hochberg Guest

    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
     
    Tim Hochberg, Oct 14, 2003
    #1
    1. Advertising

  2. Tim Hochberg

    Steve Lamb Guest

    On 2003-10-14, Tim Hochberg <> wrote:
    > 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 >>?

    --
    Steve C. Lamb | I'm your priest, I'm your shrink, I'm your
    PGP Key: 8B6E99C5 | main connection to the switchboard of souls.
    -------------------------------+---------------------------------------------
     
    Steve Lamb, Oct 14, 2003
    #2
    1. Advertising

  3. Tim Hochberg

    Tim Hochberg Guest

    Steve Lamb wrote:
    > On 2003-10-14, Tim Hochberg <> wrote:
    >
    >>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 >>?


    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
     
    Tim Hochberg, Oct 14, 2003
    #3
  4. Tim Hochberg

    Guest

    Tim Hochberg <> wrote in message news:<yjXib.26272$gi2.18311@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[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.
     
    , Oct 15, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    2
    Views:
    1,235
  2. morrell
    Replies:
    1
    Views:
    964
    roy axenov
    Oct 10, 2006
  3. MotoK
    Replies:
    59
    Views:
    1,819
    Keith Thompson
    Sep 15, 2006
  4. Replies:
    11
    Views:
    1,000
    Nindi
    Nov 14, 2006
  5. Replies:
    1
    Views:
    312
Loading...

Share This Page