[wingide-users] Wing IDE 4.0.4 released

Wingware Support support at wingware.com
Mon Dec 17 10:18:57 EST 2012


Max wrote:
> How do I get apply parens to selection to work when using vi key bindings?
> Tried selecting text (visual mode) and then simply entering "(".
> Also tried selecting text (visual mode) and going into edit mode but that just
> unselects my selection.
> I've search this mailing list and web at large. Closest I found was a vim plugin
> called "surround.vim" but I'm not sure if I can customize Wing that way?
> This would be a great feature to have within the vi key bindings.

Outside of VI mode you can turn on auto-editing in the Edit > Keyboard 
and make sure the Apply () etc To Selection option is checked.

In VI mode this doesn't work because any time there is a selection the ( 
and ) keys are not going to the text as key strokes and thus 
auto-editing is not triggered.  In this case I think the only solution 
is to add a script that does the transform and bind that to key.  This 
will work in Wing Pro only.

For example with the script I've appended below you could add a binding 
with the Keyboard > Custom Key Bindings preference:

Ctrl-Shift-(  ->  surround(char="(")

You'll need to drop the script into the scripts directory in your Wing 
IDE installation and then Reload All Scripts from the Edit menu.


import wingapi

def _transform_selection(editor, xform):
   """Change current selection to all lower case"""

   doc = editor.GetDocument()
   start, end = editor.GetSelection()
   doc.BeginUndoAction()
   try:
     if start != end:
       with_selection = 1
     else:
       with_selection = 0
       wingapi.gApplication.ExecuteCommand("forward-word")
       end, dummy = editor.GetSelection()
     txt = doc.GetCharRange(start, end)
     new_txt = xform(txt)
     if new_txt != txt:
         doc.DeleteChars(start, end-1)
         doc.InsertChars(start, new_txt)
   finally:
     doc.EndUndoAction()
   if with_selection:
     editor.SetSelection(start, start+len(new_txt))
   else:
     editor.SetSelection(end, end)

def surround(char):
   """Surround selected text with (), [], {}, "", or ''.  Arg char
   should be the opening character."""

   ed = wingapi.gApplication.GetActiveEditor()
   if ed is None:
     return

   pairs = "()[]{}''\"\""
   pos = pairs.find(char)
   if pos == -1:
     return
   closing = pairs[pos+1]

   def xform(x):
     return char + x + closing

   _transform_selection(ed, xform)


-- 

Stephan Deibel
Wingware | Python IDE
Advancing Software Development

www.wingware.com


More information about the wingide-users mailing list