In This Article

Macro Recording and Playback

SyntaxEditor has full macro recording and playback capabilities. It can record any edit action that is executed and can play back the set of edit actions at a later time. During macro recording mode, mouse input to the editor is ignored.

Controlling Macro Recording

All macro recording functionality is accessible via the SyntaxEditor.MacroRecording property. The IMacroRecording interface has a number of important members:

Member Description
Cancel Method Cancels recording a macro. When macro recording is cancelled, the old value of the LastMacroAction property is not overwritten.
CurrentMacroAction Property Gets the IMacroAction that is currently being recorded. This method returns null if no macro recording is currently taking place.
LastMacroAction Property Gets the last IMacroAction that was recorded. After macro recording is stopped, this property contains what was recorded.
Pause Method Pauses recording a macro. Use the Resume method to resume recording.
Resume Method Resumes recording a macro from a paused state. This method should be called after a Pause to resume recording.
Record Method Starts recording a new macro. Use the Stop method to end recording and save the macro that was recorded.
State Property Gets a MacroRecordingState indicating the current state of macro recording.
Stop Method Stops recording a macro. The IMacroAction that is recorded is stored in the LastMacroAction property. If no commands were recorded during this recording session, the old value of the LastMacroAction property will not be overwritten.

All of the methods above raise the SyntaxEditor.MacroRecordingStateChanged event.

IMacroAction

Once a macro has been recorded, it is stored in a IMacroAction-based object. The IMacroAction interface is an IEditAction itself, with MacroAction being the default implementation of the interface. It and the other macro-related edit actions override the CanRecordInMacro property to prevent them from being recorded in other macros.

To play back a IMacroAction, execute code like this:

editor.ActiveView.ExecuteEditAction(macroAction);

The MacroAction class is an enumerable class of child IEditAction objects.

Adding Macro Edit Action Key Bindings

SyntaxEditor ships with several edit actions that can be added as key bindings to the SyntaxEditor.InputBindings collection:

Command Description
CancelMacroRecordingAction Cancels recording a macro.
PauseResumeMacroRecordingAction Pauses or resumes recording a macro, depending on the current state of macro recording.
RunMacroAction Runs the macro that was last recorded.
ToggleMacroRecordingAction Starts or stops recording a macro, depending on the current state of macro recording.

By default, these edit actions are not added to the default key bindings.

Macro Commands

The EditorCommands class has a number of static properties containing commands that are handled by SyntaxEditor.

These commands are related to macros and can be used by toolbar buttons, etc. to control macro recording:

The commands use the related edit actions described in the previous section.

Serializing and Deserializing MacroActions to XML

SyntaxEditor can serialize a macro to XML by using the MacroAction.WriteToXml method. To deserialize a macro later, create an instance of the MacroAction and call its ReadFromXml method. All the child edit actions of the macro should load into the macro so that it is ready to be run.

Injecting Custom Macro Action Implementations

It is possible to use your own custom IMacroAction implementation when you wish to create even more advanced macro functionality. This could be done in scenarios where you wish to implement additional functionality like conditionals, looping, or nesting. The first step is to build out a custom class that implements IMacroAction.

Next, create a class that implements IMacroRecording. This IMacroRecording object is responsible for managing macro recording within the editor and also creates a new IMacroAction when recording begins. The easiest way to do this is to inherit our default MacroRecording class and override its virtual CreateMacroAction method to create your custom class that implements IMacroAction. The NotifyEditAction method is called whenever an IEditAction is about to execute in a view, allowing it to be recorded if appropriate.

The final step is to override the virtual SyntaxEditor.CreateMacroRecording and return an instance of your custom IMacroRecording class.

Now that your custom IMacroRecording and IMacroAction objects are in use, you have full control over the macro recording process.