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

Wing IDE Support support at wingware.com
Tue Sep 27 13:51:09 MDT 2011


On 9/26/11 5:26 PM, Matthew Zaleski wrote:
> Dredging up an old topic of mine since I never got this working.
>
> Is there any solution to the asynchronous editor behavior I was seeing?  A
> smart-copy macro is pretty useless without a smart-paste macro that works.

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.

Thanks,

John

----------------

# Last line that was smart copied; never contains an eol
SMART_COPIED_LINE = None

def smart_copy():
   """Implement a variant of clipboard copy that copies 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('copy')
     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)
     else:
       end = doc.GetLineEnd(lineno)
     editor.SetSelection(start, end)
     app.ExecuteCommand('copy')
     editor.SetSelection(selection[0], selection[1])
     SMART_COPIED_LINE = doc.GetCharRange(start, end)
     if SMART_COPIED_LINE.endswith(editor.GetEol()):
       SMART_COPIED_LINE = SMART_COPIED_LINE[:-len(editor.GetEol())]

def smart_paste(editor=wingapi.kArgEditor):
   """ A variant of paste that inserts line just copied with smart-copy 
above
   current line. """

   from guiutils import wgtk
   global SMART_COPIED_LINE

   clip = wgtk.clipboard_get()
   txt = clip.wait_for_text()
   selection = editor.GetSelection()

   txt_lines = (txt.splitlines() if txt is not None else ())
   if txt is None or selection[0] != selection[1] or len(txt_lines) != 1 \
      or txt_lines[0] != SMART_COPIED_LINE:
     wingapi.gApplication.ExecuteCommand('paste')
   else:
     doc = editor.GetDocument()
     lineno = doc.GetLineNumberFromPosition(selection[0])
     start = doc.GetLineStart(lineno)
     column = selection[0] - start

     doc.InsertChars(start, txt_lines[0] + editor.GetEol())


More information about the wingide-users mailing list