[wingide-users] deleting spaces; Wing API

Wingware Support support at wingware.com
Fri Mar 19 13:38:26 MDT 2010

Mitchell L Model wrote:
> Please critique this code from the standpoint of appropriate use of 
> scripting mechanisms?
>     def just_one_space(count=wingapi.kArgNumericModifier(4),
>     app=wingapi.kArgApplication):
>         """[from Emacs] Delete all spaces and tabs around point, leaving
>     one space (or n spaces)"""
>         editor = app.GetActiveEditor()
>         doc = editor.GetDocument()
>         start, end = editor.GetSelection()
>         while doc.GetCharRange(start-1, start) in (' ', '\t'):
>             start -= 1
>         while doc.GetCharRange(end, end+1) in (' ', '\t'):
>             end += 1
>         doc.BeginUndoAction()
>         doc.DeleteChars(start, end-1)
>         doc.InsertChars(start, ' ' * max(1, count))
>         doc.EndUndoAction()
>         editor.ExecuteCommand("forward_char")
> Also, providing a count argument by typing Escape and a number doesn't 
> change its behavior even though the InsertChars is supposed to insert 
> that many characters. What did I do wrong?

The Esc command isn't set up to feed the repeat to commands as an
arg; it just calls the command repeatedly.  We should probably change
that.  The kArgNumericModifier stuff was added for the vi mode and
the implementation was not propagated into emacs mode (mainly b/c
it never came up before and I didn't think of it at the time).
I'll look into adding this in the next release.

If you change the 4 to 1 in the above, it seems to work and overall
looks about how I would expect a script like this to use the API.

You may want to add conditions to the while loops so that start or
end don't go out of range.


Stephan Deibel
Wingware | Python IDE
Advancing Software Development


More information about the wingide-users mailing list