« Event registration details in C# | Main | Combat Hand Signals »

Creating Useful Temporary Macros

In classic text editors such as Emacs and Vi it is a very simple operation to create a temporary macro to repeat complex editing sequences.

Remarkably, in Visual Studio 2005 it is still relatively hard to do this successfully. The general problem is that there are many commands which don’t record and playback accurately.

The key operation in creating many useful temporary macros is being able to use a variety of commands to locate the beginning and end of a selection. Holding down the shift key while using simple movement commands extends a selection but there’s no way to extend the selection when using Incremental Search (Ctrl-I) or regular Find (Ctrl-F). In these situations, the classic solution is to position a mark at the beginning of the selection, move to the end, and then set a selection between the mark and the active position.

Here are some simple macros that set a mark based on the current active position and set the selection range from that mark to the active position.

    Dim markPoint As EnvDTE.EditPoint

 

    Sub Mark()

        markPoint = DTE.ActiveDocument.Selection.ActivePoint.CreateEditPoint

    End Sub

 

    Sub SelectToMark()

        Dim selection As EnvDTE.TextSelection

        Dim activePoint As EnvDTE.EditPoint

        selection = DTE.ActiveDocument.Selection

        activePoint = selection.ActivePoint.CreateEditPoint

 

        selection.MoveToPoint(markPoint)

        selection.MoveToPoint(activePoint, True)

 

    End Sub

By assigning keyboard shortcuts to these two commands it is possible to create complex temporary macros that don’t require editing and which work on the first try.

There are a number of existing commands in the IDE which hint at this functionality but for which I haven’t been able to find a complete working scenario. The commands and the open issues are:

Command

Issues

Edit.SelectToLastGoBack

Haven’t found a means of reliably knowing when a “GoBack” point is being set.
Doesn’t record and playback accurately.

Edit.SwapAnchor

Haven’t found a SetAnchor command.

Edit.EmacsSetMark

Haven’t found an Edit.EmacsSelectToMark command.

 

 

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)