[wingide-users] Replace in selection

Gigi the.gigi at gmail.com
Sat Aug 6 00:35:58 EDT 2005

One thing that bothered me in WingIDE is that it is not possible to
perform replace in the selected test only. This is the most useful
mode IMO and it's the default in many IDEs. I noticed a thread from
April 2005 (search/replace revisited...again) that addresses the same
problem. Well, luckilly WingIDe is scriptable and it turned out that
is is pretty simple to extend it to support replace in selection. Here
is what I did:

1. Added the following function to editor-extensions.py in WingIDE's
scripts directory:

def replace_in_selection(what, with_what):
  """Replaces 'what' with 'with_what' in the current selection only"""
  def trasform(text):
    return text.replace(what, with_what)
  app = wingapi.gApplication
  editor = app.GetActiveEditor()
  _transform_selection(editor, transform)

2. Modified the _transform_selection function to select the
transformed test after the transformation. The original function
assumed that the transformation will retain the text length (e.g.
lowercase, uppercase or convert underscore to hyphens). This
assumption breaks when the transformation replaces some words in the
selection. Here is the change in the last line of

-  editor.SetSelection(start, end)
+ editor.SetSelection(start, start+len(txt))

3. I bound the new replace-in-selection command (underscores turn to
hyphens under the WingIDE scripting API) to the ctrl+H key combination

Now, whenever I press ctrl+H WingIDE displays two edit fields at the
bottom of the main window labeld: 'what' and 'With what'. When I
populate these fields and press enter it replaces evey occurence of
expression I put in 'what' with the expression I put in 'with what'.
Note that no regular expressions are used, just plain old replace.

Still, I think it would be nice if the next version of WingIDE
integrates this common replace mode in the main search/replace

Thrive, Gigi

More information about the wingide-users mailing list