[wingide-users] RE: inserting comments on a line

Wingware Support support at wingware.com
Mon Dec 14 14:25:31 MST 2009

Warren, Russell wrote:
> Mitchell L Model wrote:
>> I am returning to serious Wing use after several years of only 
>> occasional light use. I think I am missing something regarding 
>> comments: what's the easiest way to insert a comment on the current 
>> Python line? More specifically, I would like comments to all start at 
>> a designated column as they do in Emacs. The only thing I seem to be 
>> able to do now is hold the spacebar down until the cursor gets near 
>> the column I want, type a # and a space, then my comment. There must 
>> be something better (?).
> A few years back someone already made an extension that did exactly
> this.  It was a great script.  I've recently been lamenting its loss
> after a few pc replacements where I foolishly didn't keep my wing
> scripts for one of them.
> You should be able to search the old wing mailing list entries to find
> it, but I haven't had much luck.  It was quite a while ago.

I couldn't find it either but here's an implementation.  I've added this
to our scripts/editor_extensions.py examples so it won't get lost again.

You can bind a key to use it to one of the following:


The first one uses the defined tab size for the file (depends on preferences
and in some cases file content).  The second one would be how you set a fixed
column for comments.

The script is:

import wingapi

def insert_spaces_to_tab_stop(tab_size=0):
   """Insert spaces to reach the next tab stop (units of given tab size
   or editor's tab size if none is given)"""

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

   if tab_size == 0:
     tab_size = ed.GetTabSize()

   start, end = ed.GetSelection()
   lineno = doc.GetLineNumberFromPosition(start)
   line_start = doc.GetLineStart(lineno)
   offset = start - line_start
   remainder = offset % tab_size

   insert = ' ' * (tab_size - remainder)
   if start != end:
     doc.DeleteChars(start, end-1)
   doc.InsertChars(start, ' ' * (tab_size - remainder))

   ed.SetSelection(start + (tab_size - remainder), start + (tab_size - remainder))

Hope that helps.


Stephan Deibel
Wingware | Python IDE
Advancing Software Development


More information about the wingide-users mailing list