[wingide-users] Re: Implementing Smart-Cut and Smart-Copy macros

Matthew Zaleski matthew.zaleski at spamcop.net
Wed Oct 5 15:38:47 MDT 2011


Wing IDE Support <support <at> wingware.com> writes:

> 
> I took another try at implementing this and think I came up with 
> something that should usually work.  The code is below and replaces the 
> current smart_copy implementation in editor-extensions.py.
> 
> I'm not sure I got the correct behavior for smart-paste with regards to 
> the selection after the line insertion so please let me know if it 
> should do something different.
> 

Sorry for the delay.  GMane does not display old resurrected threads in a      
manner I expected and your direct reply got filtered by my email package.

The smart_copy() and smart_paste() seem to be working exactly as I expected.
I tried a variety of boundary conditions and all behaved the same as my other
editors.  I then tried to create a complimentary smart_cut() and failed
miserably.  I'm not sure if I'm being bitten by the same bug that started this
thread (asynchronous handling of app.ExecuteCommand() calls).  Here is what I 
wrote:

def smart_cut():
    """Implement a variant of clipboard cut that cuts the whole
    current line if there is no selection on the editor."""
   
    global SMART_COPIED_LINE
   
       
    app = wingapi.gApplication
    editor = app.GetActiveEditor()
    if editor is None:
        return
    selection = editor.GetSelection()
    if selection[1] - selection[0] > 0:
        app.ExecuteCommand('cut')
        SMART_COPIED_LINE = None
    else:
        doc = editor.GetDocument()
        lineno = doc.GetLineNumberFromPosition(selection[0])
        start = doc.GetLineStart(lineno)
        if lineno + 1 < doc.GetLineCount():
            end = doc.GetLineStart(lineno + 1)
            column = selection[0] - start
            start_next_line = doc.GetLineStart(lineno + 1)
            end_next_line = doc.GetLineEnd(lineno + 1)
            if start_next_line + column > end_next_line:
                new_caret = end_next_line
            else:
                new_caret = start_next_line + column   
        else:                                          
            end = doc.GetLineEnd(lineno)
            new_selection = (start, start)
        editor.SetSelection(start, end)
        SMART_COPIED_LINE = doc.GetCharRange(start, end)
        if SMART_COPIED_LINE.endswith(editor.GetEol()):
            SMART_COPIED_LINE = SMART_COPIED_LINE[:-len(editor.GetEol())]
        app.ExecuteCommand('cut')
        editor.SetSelection(new_caret, new_caret)






More information about the wingide-users mailing list